Javascript DOM HTML document images Collection get by namedItem(id)

Introduction

namedItem(id)

Get the URL of the <img> element with id="myImg" in the document:

var x = document.images.namedItem("myImg").src;

Click the button to display the URL of the img element with id="myImg".

View in separate window

<!DOCTYPE html>
<html>
<body>

<img src="image1.png" alt="flower" width="150" height="113">
<img src="image2.png" alt="flower" width="152" height="128">
<img id="myImg" src="image3.png" alt="Smiley face" width="42" height="42">
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*  www . j  a  v a2 s . c  o m*/
  var x = document.images.namedItem("myImg").src;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related