Javascript DOM HTML Image width Property get

Introduction

Return the width of an image:

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

Click the button to display the width of the image.

View in separate window

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {/* w  ww. j a  v a2  s .  c om*/
  var x = document.getElementById("myImg").width;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related