steve Kim steve Kim - 2 months ago 7
jQuery Question

jQuery determine number of div

I have the following:

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


I need to determine the number of div with the class
item
only after "
item second
" div? In this case, it will be 3.

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


How would I go about?

Answer

With $('div.item.second').nextAll('.item').length

console.log($('div.item.second').nextAll('.item').length)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="item"></div>
<div class="item second"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>