Javascript Style How to - Return the style properties for width








Question

We would like to know how to return the style properties for width.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#foo {<!--from   w  w w.java2s.  c  o m-->
  background: green;
  width: 80px;
}
</style>
<script type='text/javascript'>
function getWidth() {
    var elem = document.getElementById("foo");
    var theCSSprop = window.getComputedStyle(elem, null).getPropertyValue("width");
    console.log(theCSSprop);
}

</script>
</head>
<body>
  <div id="foo" onClick="getWidth()">Hello World. click and check the console.</div>
</body>
</html>

The code above is rendered as follows: