0

This works:

<div id="hello" style="width:100%;"></div>
<script>
window.onload = function(){

alert(document.getElementById("hello").style.width);

};
</script>

This does not work:

<div id="hello"></div>
<style>
div#hello{
  width:100%;
}
</style>
<script>
window.onload = function(){

alert(document.getElementById("hello").style.width);

};
</script>
  • I have also tried properly putting the css style definition in the head tag, didn't work
  • I have tried defining a javascript function instead of calling on window.onload , didn't work

strangest thing is, if I set the width using javascript:

alert( document.getElementById("hello").style.width );
document.getElementById("hello").style.width = "25%";
alert(document.getElementById("hello").style.width );

It would work. The first alert would show a blank alert, then the second alert will show "25%"

Gapton
  • 2,044
  • 2
  • 20
  • 33
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1098349/reading-non-inline-css-style-info-from-javascript – Alexander Pavlov Mar 06 '12 at 15:32
  • possible duplicate of [Read CSS property of an element using JavaScript](http://stackoverflow.com/questions/7894577/read-css-property-of-an-element-using-javascript) – Blazemonger Apr 30 '14 at 17:51

7 Answers7

2

Try the following

window.onload = function(){

var x = document.getElementById("hello");
var y ="";
if (x.currentStyle)
    y = x.currentStyle['width'];
else if (window.getComputedStyle)
    y = document.defaultView.getComputedStyle(x,null).getPropertyValue('width');
alert(y);



};

It was inspired from something I read Here

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
1

In InternetExplorer you can use .currentStyle instead of .style. In other browsers, you can use the getComputedStyle mechanism.

var yourElement = document.getElementById('whatever');
var theStyle = window.getComputedStyle(yourElement);

You can then call .getPropertyValue() on the returned style object to find the CSS properties you're interested in.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

You cannot access CSS properties that way that have not been set using Javascript. You need to use getComputedStyle, see https://developer.mozilla.org/en/DOM/window.getComputedStyle On MSIE this works differently.

  • Thanks that was informative. I wasn't aware of getComputedStyle and the associated cross-browser problem. Will keep this in mind as a web dev from now on. I chose to use jQuery to overcome this in the end. – Gapton Mar 07 '12 at 03:14
  • *"...that have not been set using Javascript."* That's incorrect. It's not that they've been set by JavaScript; it's that they're directly applied to the element. That *could* be via JavaScript, or via the `style` attribute in markup. – T.J. Crowder May 04 '17 at 22:07
0

Try to put your css in the head section of the page.

ihorko
  • 6,855
  • 25
  • 77
  • 116
  • if you have read my post you would know that is something i have already tried. and no it doesn't work. – Gapton Mar 06 '12 at 15:57
0

I don't think style.width is given a value unless you explicitly define it one. As others have said you can use currentStyle/getComputedStyle to get actual values of CSS items.

If you need JUST the width you can go with element.offsetWidth. If you need the value as a percent you'll have to compare the elements offsetWidth with the parents offsetWidth.

Snuffleupagus
  • 6,365
  • 3
  • 26
  • 36
0

jQuery will do it for you

$('#hello').mouseover(function({
  alert($(this).width()); 
});

Just try it!

Jannis M
  • 745
  • 1
  • 4
  • 15
0

Are you adverse to using jQuery?

http://api.jquery.com/width/

$(document).ready(function(){
    alert($("#hello").width());
});
tedski
  • 2,271
  • 3
  • 27
  • 48