[jquery] jquery $('.class').each() how many items?

When looping over a group of items using jquery selectors is there a way to find out how many items there are on the collection?

This question is related to jquery jquery-selectors

The answer is


Use the .length property. It is not a function.

alert($('.class').length); // alerts a nonnegative number 

You mean like length or size()?

Refs: http://api.jquery.com/length/


If you are using a version of jQuery that is less than version 1.8 you can use the $('.class').size() which takes zero parameters. See documentation for more information on .size() method.

However if you are using (or plan to upgrade) to 1.8 or greater you can use $('.class').length property. See documentation for more information on .length property.