2

I am using jQuery to try and get the width of an element that doesn't have the width set. In firefox it works fine, but in IE it returns AUTO instead of the actual width of the element.

jQuery('.nav-2').css('width');

Does anyone know of a work around or another way to get the width of the element that will work in IE?

1
  • Did you know that you only have to write jQuery in its long form once? By wrapping your code in (function($) { .... })(jQuery);, you can use $ no matter if noConflict has been used or not. Commented Apr 16, 2012 at 12:04

3 Answers 3

7

Call .width() instead.

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

Comments

1

You don't want the CSS width as the style is probably actually set to "auto". You want the actual object width which is obtained in jQuery with this:

jQuery('.nav-2').width();

Keep in mind that when obtaining the width using a selector that can return multiple elements, jQuery will return the width of the first matched element.

2 Comments

When I use that it returns a different number than firefox. In firefox it returns 136 and in IE it returns 1899, do you have any idea why?
You would have to show us your HTML/CSS (use Edit in your original question to insert it) so we could see why the HTML might behave differently in one browser vs. the other. 1899 sounds like it's probably full width whereas 136 is not. There are reasons for that, but they depend upon how you've done your HTML/CSS. It's not possible to guess.
0

Get the total width (padding + width + borders + margin)

 jQuery('.nav-2').outerWidth(true);

Remove 'true' to exclude margins

Comments