Document images Collection - Loop through all <img> elements in the document, and output the URL (src) of each image: - Javascript DOM

Javascript examples for DOM:Document images

Description

Document images Collection - Loop through all <img> elements in the document, and output the URL (src) of each image:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<img src="http://java2s.com/resources/a.png" alt="flower" width="150" height="113">
<img src="http://java2s.com/resources/b.png" alt="flower" width="152" height="128">
<img src="http://java2s.com/resources/c.png" alt="Smiley face" width="42" height="42">

<button type="button" onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*  ww w. j  a  va2 s . c o  m*/
    var x = document.images;
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        txt = txt +  x[i].src + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials