Jquery which sibling number has class

Viewed 20

I want to know wich sibling number has current class in a list:

<ul>
<li>Item 1</li>
<li>Item 1</li>
<li class="current">Item 1</li>
<li>Item 1</li>
</ul>

Result must be 3. Any ideas? Thanks a lot!

1 Answers

the start index is 0, so you can use this way to get your number.

var currentIndex = $('ul li.current').index(); // get index 2
console.info(currentIndex + 1);                // your number
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>Item 1</li>
  <li>Item 1</li>
  <li class="current">Item 1</li>
  <li>Item 1</li>
</ul>

Related