jQuery Style How to - Get element width and height








Question

We would like to know how to get element width and height.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.0.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  w  w w.  j a  v a 2  s . com-->
    var $img = $('.foo')
    var $width = $img.width();
    var $height = $img.height();
    console.log($width );
    console.log($height);
});
</script>
</head>
<body>
  <div class='foo'>foo</div>
</body>
</html>

The code above is rendered as follows: