Get the count of div elements after current div

Viewed 210
<div class="main">
      <div class="item"></div>
      <div class="item active"></div>
      <div class="item"></div>
      <div class="item"></div>
</div>

Now i want the number of childrens of main class, which will be 4 in this case but i want the count that after the current (active class), which will be 2. how to do that ??

js:

var a = $('.main').find('.item').after('.active').length; 
2 Answers

This should work

var len =$("div.main .item.active").nextAll().length

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

This works:

let items = $('.main .item');
let total = items.length;
let passed = 0;
for (let i in items) {
  passed = +i + 1;
  if ($(items[i]).hasClass('active'))
    break;
}
console.log(total - passed);
Related