jQuery HTML Element How to - Get an image's width which's auto in css








Question

We would like to know how to get an image's width which's auto in css.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>
$(function(){<!--  w w w .  j  a  va 2  s .  co m-->
    $(document).ready(function() {
        $("img").load(function() {
            console.log($(this).height());
            console.log($(this).width());
        });
    });
});
</script>
</head>
<body>
  <img src="http://www.java2s.com/style/download.png"
    alt="Smiley face">
</body>
</html>

The code above is rendered as follows: