Get image id: javascript
Solved
Hello,
I would like to retrieve the id of one of the images.
Html:
<a href = "redirection()">Click here</a>
But I would like to retrieve the id of one of the images using JavaScript (redirection function). I can't find the method?
Thank you in advance
Configuration: Windows XP / Internet Explorer 8.0
I would like to retrieve the id of one of the images.
Html:
<div id = "conteneur"> <img src = "image/toto" id = "image1" /> <img src = "image/tata" id = "image2" /> <img src = "image/tutu" id = "image3" /> </div>
<a href = "redirection()">Click here</a>
But I would like to retrieve the id of one of the images using JavaScript (redirection function). I can't find the method?
Thank you in advance
Configuration: Windows XP / Internet Explorer 8.0
5 answers
-
Use the getElementsByTagName function ( https://www.toutjavascript.com/reference/ref-window.document.getelementsbytagname.php )
A quick example:function getImgs(){ var ImgArray = document.getElementsByTagName('img'); var mystring = ''; for(var i = 0; i < ImgArray.length; i++){ mystring += "("+i+") = "+ImgArray[i].id+" - "; } alert('String : '+mystring); }