HP.
HP.

Reputation: 19896

jQuery width() function returns wrong value in Internet Explorer 9

I used Easy Slider jQuery plugin before and never had problem until now. And the problem is strange. Check out this home page http://bit.ly/HKiWY6

The page will pop an alert showing two values:

$("#slider").width()

and

$("#slider3").width()

I already set the value for both in css. One is 710px and one is 700px.

If you run in IE9, it shows the default value of $(window).width() instead for both, whatever the window or document width currently is. FF and Chrome returned correctly. Why is that?

Upvotes: 4

Views: 12605

Answers (2)

Phradion
Phradion

Reputation: 431

Try the outerWidth, and make sure to wrap it in a windows.ready event listener to make sure all DOM element rendered properly before the width being computed

$("#slider3").outerWidth()

Upvotes: 10

Casey Foster
Casey Foster

Reputation: 6010

I've had problems with jQuery's width()/height()/offset().top/.left etc in the past when I used them before a certain event fully bubbled. See if setTimeout(function() { alert($('#slider').width()); }, 0); has any effect. It's a cheap nextTick() trick that might be just what you need.

Upvotes: 6

Related Questions