jQuery count number of divs with a certain class?

Considering something like this;

<div class="wrapper">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

How would I, using jQuery (or plain JS, if it’s shorter – but I doubt it) count the number of divs with the “item” class? In this example, the function should return 5, as there are 5 divs with the item class.

Thanks!

Answer

You can use the jquery .length property

var numItems = $('.item').length;

Leave a Reply

Your email address will not be published. Required fields are marked *