Q:

How to get current image size (width & height) in javascript

0

How to get current image size (width & height) in javascript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the JavaScript clientWidth property

You can simply use the JavaScript clientWidth property to get the current width and height of an image. This property will round the value to an integer.

Let's check out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Find Current Image Width and Height</title>
<script>
    function imgSize(){
        var myImg = document.querySelector("#sky");
        var currWidth = myImg.clientWidth;
        var currHeight = myImg.clientHeight;
        alert("Current width=" + currWidth + ", " + "Original height=" + currHeight);
    }
</script>
</head>
<body>
    <img src="sky.jpg" id="sky" width="250" alt="Cloudy Sky">
    <p><button type="button" onclick="imgSize();">Get Current Image Size</button></p>
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to get original image size (width & height) in... >>
<< Show read more link if the text exceeds a certain ...