How do I get the number of elements that have the particular class .added
when the element with the class .header
is clicked?
<ul class="main">
<li class="header">Header</li>
<li class="added">One</li>
<li>Two</li>
<li class="added">Three</li>
<li>Four</li>
</ul>
So in this case, when I click the element with the class .header
I would want to retrieve the number 2.
length
should work. size()
is deprecated as of jQuery 1.8.
Example:
$('.header').on('click', function () {
return $('.added').length;
});
See: http://api.jquery.com/length/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With