Try to use the method "offsetWidth":
<ul id="MyElement" style="display:block;visibility:hidden;">
<li>Hello World!</li>
<li>Hellow Web!</li>
</ul>
<script type="text/javascript">
alert(document.getElementById('MyElement').offsetWidth);
</script>
It should return an element's dynamic width.
Or if you want jQuery try to add this to your jQuery file (jquery.js)
jQuery.fn.offsetWidth = function () {
return this.offsetWidth;
}
If it's set to hidden (seems to be the problem) and still returns 0 as width, try to clone the element (the element you want to get the width from). Set css position:absolute; left:-200; top -200;visibility:visible;
of your element copy. Get the width of the element copy either jQuery or just JavaScript. Remove the element copy. You should now get the width. The top
and left
is set negative so the element copy won't appear on your screen.
Note that this can make your execution a little bit slower.
` in a visible parent?
– jerluc Jul 04 '11 at 00:39