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%"

2

7 Answers 7

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but this will not work on IE I think. I used jQuery to sort out the cross-browser issue in the end.
I have tested it in IE 8 and it works. Glad you found a solution using jQuery.
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.

2 Comments

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.
"...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.
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.

Comments

0

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

1 Comment

if you have read my post you would know that is something i have already tried. and no it doesn't work.
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.

Comments

0

jQuery will do it for you

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

Just try it!

Comments

0

Are you adverse to using jQuery?

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

$(document).ready(function(){
    alert($("#hello").width());
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.