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

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the JavaScript clientWidth property

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

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="UTF-8">
  • <title>JavaScript Find Current Image Width and Height</title>
  • <script type="text/javascript">
  •     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" contextmenu="skymenu">
  •     <p><button type="button" onclick="imgSize();">Get Current Image Size</button></p>
  • </body>
  • </html>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.