Image naturalWidth Property - The difference between the naturalWidth property and the width property: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image

Description

Image naturalWidth Property - The difference between the naturalWidth property and the width property:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<img id="myImg" src="http://java2s.com/resources/a.png" style="width:500px;height:98px;">

<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {//from  w  ww . ja v  a 2 s  . c  o m
    var x = document.getElementById("myImg").naturalWidth;
    document.getElementById("demo").innerHTML = "The original width of the image is: " + x + " pixels";

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

</body>
</html>

Related Tutorials