Alex Rivera | Logout

Count elements with jQuery

Asked 2011-04-18T16:44:45.007
147

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

Edit
Report

2 Answers

294
$('.someclass').length

You could also use:

$('.someclass').size()

.size() is now removed as of jQuery 3.0.

answered 2011-04-18T16:47:06.290
29
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/

answered 2011-04-18T16:46:11.577

Your Answer