Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 460
HP. Avatar asked Dec 04 '22 16:12

HP.


2 Answers

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()
like image 188
Phradion Avatar answered Dec 22 '22 00:12

Phradion


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.

like image 21
Casey Foster Avatar answered Dec 22 '22 00:12

Casey Foster