Javascript DOM HTML Image compare naturalHeight property and the height property

Introduction

The difference between the naturalHeight property and the height property:

Click the button to return the original height and the "new/styled" height of the image.

View in separate window

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {/*from  ww w  .  j  a v  a 2  s.  co  m*/
  var x = document.getElementById("myImg").naturalHeight;
  document.getElementById("demo").innerHTML = "The original height of the image is: " + x + " pixels";

  var y = document.getElementById("myImg").height;
  document.getElementById("demo2").innerHTML = "The 'styled' height of the image is: " + y + " pixels";
}
</script>

</body>
</html>



PreviousNext

Related