0

I would like to know the index of the li elements, just which has a certain class.

<ul>
    <li style="display: none;">don't need this one</li>
    <li class="li">sdsds</li>
    <li class="li">sdsds</li>
    <li class="li">sdsds</li>
</ul>

$('ul').each(function () {
    $(this).on('click', 'li.li', function () {
        alert($(this).index());
    });
});

http://jsfiddle.net/oobkzyat/2/

My problem is that the index is not starting with 0. How can I do it?

1
  • Subtract the number one? The problem is if you remove arbitrary elements the list of elements returned will only be the ones you've selected. If you need an unchanging value when the DOM you're picking from can change then either select a different way, or use a data- attribute, or... etc. Commented Jun 22, 2015 at 19:09

4 Answers 4

2

Either element displays or not. It will be a part on the index that's the reason it gives you 1 instead 0 because 0 is the index of the element you hid (<li style="display: none;">don't need this one</li>) . However below is code for your requirement.

$('ul').each(function () {
    $(this).on('click', 'li.li', function () {
      alert($(this).index()-$(this).prevAll(":hidden").length);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<ul>
    <li style="display: none;">don't need this one</li>
    <li class="li">sdsds</li>
    <li class="li">sdsds</li>
    <li class="li">sdsds</li>
</ul>

Sign up to request clarification or add additional context in comments.

Comments

1

If you look at the documentation, you will notice the other way to use index() : .index() can search for this list item within the set of matched elements. Here's an example how to use it:

$('ul').each(function () {
  
  var classLi = $(this).find('> li.li');
  // get direct children with class "li"
  
  classLi.on('click', function () {
    alert(classLi.index(this));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul>
    <li style="display: none;">don't need this one</li>
    <li class="li">sdsds</li>
    <li class="li">sdsds</li>
    <li class="li">sdsds</li>
</ul>

Comments

0

You need to query for visible elements using :visible selector.

alert($(this).siblings(":visible").andSelf().index(this));

I have updated your fiddle http://jsfiddle.net/oobkzyat/3/

Comments

0

Your index is starting at 1 because the 0 element does not have class="li" on it. The first element returned is the first class="li" but that is the SECOND li in the ul.

EDIT: If there are several (unknown number) hidden li that you want to ignore, you could do something like this

    $('ul').on('click', 'li.li', function(){
      var counter = 0;
      var clickIndex = $(this).index();
      // uses an ID on the ul in case there are other li on page
      $('#topUL').find('li').each(function(){
          if( $(this).index() < clickIndex && !$(this).hasClass('li') )
             counter++;
      });
      alert(clickIndex - counter);

    });

2 Comments

I know, but I would like to 'lie', that the first li.li index is 0. This is just an example, the task is more complex.
Well, if you know there is always one above it then subtract 1. If you don't know how many are above it then you'll have to get sneakier. Is this the case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.