Count the number of divs for each container in jquery

How can I count the number of divs with a slide class in each set. In the example below, the first group "element" will be with three, and the second group will have two. I am trying to use .length, but it adds all the “slides” of the div.

<div class="item">
  <div class="foo"></div>
  <div class="bar">
     <div class="slide"></div>
     <div class="slide"></div>
     <div class="slide"></div>
  </div>
</div><!-- /item -->

<div id="item">
  <div class="foo"></div>
  <div class="bar">
     <div class="slide"></div>
     <div class="slide"></div>
  </div>
</div><!-- /item -->
+3
source share
1 answer

This will give you an array of .slidedivs counters in each .item, using .map:

var counts = $(".item").map(function() {
    return $(this).find(".slide").length;
}).get();

. "item" "item" . . , .bar .item , - .

+5

Source: https://habr.com/ru/post/1756305/


All Articles