jQuery Style How to - Get Computed Style








Question

We would like to know how to get Computed Style.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.4.js'></script>
<style type='text/css'>
#foo {<!--from w  ww .  j a v  a  2s  .com-->
  display: none;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    var foo = document.getElementById('foo');
    var $output = $("#output");
    $output.text(getComputedStyle(foo).display);
});
</script>
</head>
<body>
  <div id="foo">Foo Bar</div>
  <h2 id="output"></h2>
</body>
</html>

The code above is rendered as follows: