Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery width, outerWidth(), innerWidth() returning incorrect data

For some reason, I cannot get jQuery to give me reliable width data for various elements. Here is a link to a fiddle that demonstrates the problem: http://jsfiddle.net/snoopydaniels/ZDbaa/1/

I am attempting to create a multidimensional drop down menu. As you can see in the jsfiddle, the second nested menu is not positioned correctly. The jQuery that I employ to position it queries the sibling anchor element using outerWidth() to discover its width, then sest the left position of the nested list according to the returned width. When I give the sibling anchor element a set width in CSS then I get something near the correct value, but without that static width, outerWidth() returns "2."

But even when I give anchor elements a set width, outerWidth(true) still does not correctly incorporate the border of the element.

I'm about to pull my hair out. Why can't crap like this just work as advertised?

like image 216
Daniel Arant Avatar asked Aug 23 '12 05:08

Daniel Arant


People also ask

What is the difference between innerwidth and outerwidth in jQuery?

The innerWidth () returns the inner width of the first matched element. It includes padding, but not border and margin. You can try to run the following code to get the inner width in jQuery: The outerWidth () returns the outer width of the first matched element.

How to get the outer width in jQuery?

outerWidth in jQuery. The outerWidth() returns the outer width of the first matched element. It includes padding and border. Example. You can try to run the following code to get the outer width in jQuery: Live Demo

What is the use of outerwidth in HTML?

Definition and Usage. The outerWidth() method returns the outer width of the FIRST matched element. As the image below illustrates, this method includes padding and border. Tip: To include the margin, use outerWidth(true). Related methods: width() - Sets or returns the width of an element. height() - Sets or returns the height of an element.

What is the difference between outerwidth and outerheight?

The outerWidth() method returns the outer width of the FIRST matched element. As the image below illustrates, this method includes padding and border. Tip: To include the margin, use outerWidth(true). Related methods: outerHeight() - Returns the height of an element (includes padding and border).


3 Answers

The various width commands don't work for hidden elements (display: none).

If you use css to set a width, jquery will return that. Otherwise you'll get no, or meaningless, results.

The classic solution is to position the element off the screen, check the width, then hide it and put it back. (You can probably find a jquery plugin that will do that in one command.)

Sometimes there are ways of restructuring the html and/or css so you avoid this.

Sometimes you can fix the size right after showing the element.

You can also try setting the visibility to hidden, so the element gets a size, but is invisible. Depending on the your design this might work.

like image 191
Ariel Avatar answered Oct 21 '22 23:10

Ariel


I had a similar issue in receiving what appeared to be random results when calling jQuery width functions. When the browser was refreshed, I would get the correct answer about 50 percent of the time.

I made the mistake of immediately checking the width of the div within the $(document).ready() function. When I changed to calling outerWidth() within $(window).load(), the correct answer was always returned.

like image 1
Darnuhulu Avatar answered Oct 22 '22 00:10

Darnuhulu


You need to call $(window).trigger('resize') which will trigger a fake window resize event. Afterwards, the outerWidth should have the proper value.

Make sure that you are triggering the fake window resize event once your data is populated.

In other words, if you are checking the width of a <td> inside a <th> for example, make sure the table is populated and all the resizing is done before triggering the fake resize event and checking the outerWidth.

This is a workaround due to the limitations mentioned by Ariel in https://stackoverflow.com/a/12085210/774630

like image 1
Hussein El Motayam Avatar answered Oct 21 '22 23:10

Hussein El Motayam