yossi yossi - 1 month ago 9
jQuery Question

jQuery - counting elements while saving resources

I'd like to count all

.some-class
elements in my dom. but, i don't want it to be kept as a collection (it will be done many times and i only need the number).

is there another way, instead of
$(".some-class").length
or
$(".some-class").size()
?

Answer

...i don't want it to be kept as a collection (it will be done many times and i only need the number).

That's fine, just grab the length and release the collection:

var count = $(".some-class").length;

Only the length (in count) will be retained, not the collection.

Comments