Javascript DOM HTML Image Object get

Introduction

The Image object represents an HTML <img> element.

We can access an <img> element via document.getElementById():

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

Click the button to get the URL of the image.

View in separate window

<!DOCTYPE html>
<html>
<body>
<img id="myImg" src="image1.png" alt="The Circle" width="100" height="100">
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {//from  www .  ja va2  s .c o m
  var x = document.getElementById("myImg").src;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related