get a total of jquery’s .each()

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() {


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.

Leave a Reply

Your email address will not be published. Required fields are marked *