30

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?

1
  • Ammendment: The element I'm trying to calculate the width is resizable and can be hidden during the execution, therefore its width will eventually change. Commented Aug 20, 2010 at 13:40

5 Answers 5

39

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"),10); // always use a radix

or

var width = parseInt(element.style.width,10); // always use a radix

It ignores the "px" suffix so you should be good to go.

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();
});
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
Number($elem.css('width').replace(/[^\d\.]/g, '')); - a regular expression combined with a convert to number solves the px/em/% problem.
Just curious, but what's the benefit of explicitly passing in a radix? Doesn't it default to 10? Nice solution, by the way.
@Frank these days browsers are pretty good with defaults... but I'm super old and still remember issues with browsers using non 10 radix.
7

In plain JavaScript:

parseInt('100px', 10)

Works with "100em", "100%", and even with: "100". No need for any Regular Expression patterns.

Comments

2

You could remove non-numericals with a regular expression and then just convert to a number. This works no matter how you define the width (px, em, %, pt). Preserves decimal points too.

vanilla javascript

Number(elem.style.width.replace(/[^\d\.\-]/g, ''));

jQuery

Number($elem.css('width').replace(/[^\d\.\-]/g, ''));

Comments

1

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"

2 Comments

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.
or you could remove non-numericals with a regular expression. This works no matter how you define the width (px, em, %, pt) js: Number(elem.style.width.replace(/[^\d\.]/g, '')); or jQuery: Number($elem.css('width').replace(/[^\d\.]/g, ''));
0

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.

2 Comments

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

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.