Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  119] [ 3]  / answers: 1 / hits: 61039  / 14 Years ago, tue, july 27, 2010, 12:00:00

I'm using jquery's .each() to iterate over a group of li's. I need a total of all the li's matched. Is the only way to create a count variable outside the .each() and increment this inside the .each()? It doesn't seem very elegant.



var count;
$('#accordion li').each(function() {
++count;
});

More From » jquery

 Answers
31

Two options:



$('#accordion li').size(); // the jQuery way
$('#accordion li').length; // the Javascript way, which jQuery uses


Since jQuery calls length under the hood, it's faster to use that instead of the size() call.


[#96098] Friday, July 23, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrellhunterm

Total Points: 82
Total Questions: 109
Total Answers: 98

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
;