Javascript DOM HTML Image naturalHeight Property get

Introduction

Return the original height of an image:

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

Click the button to display the original height of the image.

View in separate window

<!DOCTYPE html>
<html>
<body>

<img id="myImg" src="image1.png" style="width:107px;height:100px;">
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*w ww  .java2 s  .  c o  m*/
  var x = document.getElementById("myImg").naturalHeight;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The naturalHeight property returns the original height of an image.

The naturalHeight property stores the actual height while the height property returns the styled height.

This property is read-only.

The naturalHeight property returns a Number representing the original height of an image in pixels.




PreviousNext

Related