Document images Collection namedItem(id) - Get the URL of the <img> element with id="myImg" in the document: - Javascript DOM

Javascript examples for DOM:Document images

Description

Document images Collection namedItem(id) - Get the URL of the <img> element with id="myImg" in the document:

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 id="myImg" src="http://java2s.com/resources/c.png" alt="Smiley face" width="42" height="42">

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

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

<script>
function myFunction() {//  ww  w  .java 2 s .c o m
    var x = document.images.namedItem("myImg").src;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials