Count elements with jQuery

Is there a way to count how many elements on the page with a particular class?


Solution 1:

$('.someclass').length

You could also use:

$('.someclass').size()

which is functionally equivalent, but the former is preferred. In fact, the latter is now deprecated and shouldn't be used in any new development.

Solution 2:

var count_elements = $('.class').length;

From: http://api.jquery.com/size/

The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call.

Please see:

http://api.jquery.com/size/

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

Solution 3:

Yes, there is.

$('.MyClass').size()

Solution 4:

I believe this works:

$(".MyClass").length