Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Get index of hidden element relative to its neighbors

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #187015
    Anonymous
    Inactive

    Lets say I want to get the index of each li within the unordered list bellow.

    • index 0
    • <li class=”hidden”>index 1

    • index 2
    • <li class=”hidden”>index 3

    But lets say I want to dynamically get the index of all li with “.hidden” class, How would I get the index and still be relatable to all li.

    If I do the following, it gets the index of all li with the class, but the li number 2 will have the index 0 instead of 1. and the last li with “.hidden” class will have index 1 instead of 3.

    $(“ul li”).each(function(index) {
    $(this).index() // gets index of the alis (good)
    $(“ul li.hidden”).index() // gets index, but not related to all li
    });
    So how can i get the index of the .hidden li and have them be relatable to all other index

    #187057
    itsLeon
    Participant

    To the $(ul li).each add an check to see if that element had the class hidden and if so add it to an array.

    So you get something like.
    $(‘ul li’).each(function(i, item){
    If $(item).hasClass(‘hidden’){
    //$(item).index() will be the index you want
    }
    });

    #187059
    Anonymous
    Inactive

    This returns the index as 0. I alert the class and it does get the right class of each hidden element, but not the right index.

    #187095
    itsLeon
    Participant

    @htmlcinco

    http://jsfiddle.net/keffk614/

    I hope this helps.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.