blob84 Posted November 17, 2009 Report Share Posted November 17, 2009 HI, I'm trying to change the attribute of an element,I have this function I want to change the style top and style left position of the second image cloned form the first,I think i need to use nodeValue to get the value of an attribute, but how can i do or there is another method? <html><head><script type="text/javascript">function crea() {createImgAnanas = document.createElement("img");createImgAnanas.style.position="absolute";createImgAnanas.style.top="1%";createImgAnanas.style.left="25%"createImg1.style.zIndex="3";createImgAnanas.style.border="0";createImgAnanas.setAttribute("src", "http://www.fruechteadam.com/img/Ananas.jpg");createImgAnanas.setAttribute("id", "ananas1");createImgAnanas.setAttribute("alt", "ananas");createImgAnanas.setAttribute("title", "indicazione paese produzione ananas");createImgAnanas.setAttribute("width", "150");createImgAnanas.setAttribute("height", "79"); document.getElementById("prince").appendChild(createImgAnanas);createImgAnanas1 = createImgAnanas.cloneNode(true); document.getElementById("prince").appendChild(createImgAnanas1);}</script><body id="prince"><input type="button" value="crea" onclick="crea();" /></body></html> Link to comment Share on other sites More sharing options...
dsonesuk Posted November 17, 2009 Report Share Posted November 17, 2009 (edited) why not just replace the top left with new valuescreateImgAnanas1 = createImgAnanas.cloneNode(true);createImgAnanas.setAttribute("id", "ananas2"); //need to replace id ref name as this must be uniquecreateImgAnanas1.style.top="1%";createImgAnanas1.style.left="50%"question: why add styling to image when you have assigned id ref to anyway? use css styling to this id ref<style type="text/css">#ananas1, #ananas2{border: 0pt none ; position: absolute; z-index: 3;}#ananas1{top: 1%; left: 25%;} #ananas2{top: 1%; left: 50%;}</style> Edited November 17, 2009 by dsonesuk Link to comment Share on other sites More sharing options...
blob84 Posted November 17, 2009 Author Report Share Posted November 17, 2009 ok it works I didn't use style because i want the menu more dynamic andI always add id to elements, if it doesn't work with javascript I use style, and I have the id ready. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now