Is there a way to count how many elements on the page with a particular class?
This question is related to
jquery
The best way would be to use .each()
var num = 0;
$('.className').each(function(){
num++;
});
try this:
var count_element = $('.element').length
I believe this works:
$(".MyClass").length
$('.class').length
This one does not work for me. I'd rather use this:
$('.class').children().length
I don't really know the reason why, but the second one works only for me. Somewhy, either size doesn't work.
Yes, there is.
$('.MyClass').size()
use the .size()
method or .length
attribute
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:
Source: Stackoverflow.com