views:

62

answers:

3

While trying to compute the width of an hidden element I found that jquery.width() returns 0 for that elements' width.

I found out that using jquery.css('width') would return the correct width by using the declared style width (even if that value is different from the initial stylesheet). The problem is that the css('width') method returns a string, generally in a "100px" fashion. My question resolves into: how to retrieve the number from the "100px" string? Is there an easy way?

+3  A: 

If it always returns in px format, "100px", "50px" etc (i.e. not "em" or percent), you could just...

var width = parseInt($("#myelem").css("width"));

Although deep down I'm thinking that something isn't right if $("#myelem").width() isn't working.

Note on hidden elements.

If you are adding jQuery to progressively enhance you page, the element you are calculating should be visible when the page first loads. So you should get the width before you initially hide the element. By doing this, $("#myelem").width() will work.

var myWidth = 0;

$(document).ready( function () {
    myWidth = $("#myelem").width();
    $("#myelem").hide();
});
Sohnee
The reason why `.width()` is not working is that if objects are hidden by `.hide()`(`display: none`) they don't contribute to the layout and therefore have no width.
lasseespeholt
Ah. I will supply a solution to that...
Sohnee
The element I'm trying to calculate the width is resizable and can be hidden during the execution, therefore its width will eventually change. Thus, as you suggested, I can use a variable to hold it's value which is calculated on page `.ready()` event and in every resize callback event I must re-calculate - using `.width()` - and apply the new value. Therefore, even when the element is hidden I will have the current value.
vitorhsb
A: 

Oh, I came up with:
new Number($elem.css('width').slice(0, -2));
//to extract the 'px' and return a regular number

Now I only hope that jquery allways returns the same string fashion: "100px"

vitorhsb
It will if you've defined them all as `px`. However if you've got some widths as `%`, it will come back as `50%` and your slice function will trim off the rightmost digit. You could use `indexOf` to test for `px` for safety.
Pat
A: 

I would stick to .width() because it actually gets the computed width instead of css width. Instead of hiding it with .hide() (display: none) you could hide it with .css('visible', 'hidden') then .width() should work.

from my comment If you don't want to change your .hide()´s then you could apply visible: hidden and thereafter .show() and then measure the height. After you have measured it, reverse that. Objects still affects the page layout when they are hidden by visible: hidden - beware of that.

To avoid tags which mess with the layout, you could set the position to absolute, move it to the body tag and then measure.

lasseespeholt
I agree to use the computed width. In fact the element was hidden in the load page event, plus a few more <code>.hide</code>. I changed the css from "display: none" to "visibility: hidden" and then I could read the same values using <code>.width()</code>. Super.Now, I all have to do is change all the <code>.hide()</code> excerpts into your suggested <code>.css('visible', 'hidden')</code>. I suspect that I also need to change the <code>.show()</code> into <code>.css('visibility', 'visible')</code>.Thanks.
vitorhsb
Instead of doing all that work. Then you could apply `visible: hidden` and thereafter `.show()` and then measure the width. After you have measured it, reverse that. Then you don't have do change so much. Objects still affects the page when they are hidden by `visible: hidden` - beware of that.
lasseespeholt