jQuery count number of divs with a certain class?

user825286 picture user825286 · Sep 13, 2011 · Viewed 142.1k times · Source

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

Brian Glaz picture Brian Glaz · Sep 13, 2011

You can use the jquery .length property

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