Reputation:
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
Reputation: 78520
Initiate the variable as 0. Your code seems like it should work
Upvotes: 0
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