Javascript DOM HTML Image src Property get

Introduction

Return the URL of an image:

var x = document.getElementById("myImg").src;

Click the button to display the value of the src attribute of the image.

View in separate window

<!DOCTYPE html>
<html>
<body>

<img id="myImg" src="image1.png" width="107" height="98">
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*from ww  w . j  a v a 2  s  .  co m*/
  var x = document.getElementById("myImg").src;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related