4

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.

2

3 Answers 3

12

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/

1
$('.header').on('click', function(){
  console.log($('.added').length);
});
0

How about this:-

$('.header').on('click', function () { 
return $('.added').length;
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.