user381800
user381800

Reputation:

jQuery find width of each li element?

anyone know a way to find the width of each li within the ul?

I tried

var totalWidth;
jQuery("ul li").each(function() { totalWidth += jQuery(this).outerWidth(); });

But didn't work...

Upvotes: 2

Views: 6205

Answers (2)

Joseph Marikle
Joseph Marikle

Reputation: 78520

Initiate the variable as 0. Your code seems like it should work

Upvotes: 0

user113716
user113716

Reputation: 322462

You need to initialize the variable first:

var totalWidth = 0;

...otherwise you're starting out effectively doing:

undefined += jQuery(this).outerWidth();

...which results in NaN for subsequent iterations:

NaN += jQuery(this).outerWidth();

Upvotes: 7

Related Questions