Forums

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

Home Forums JavaScript AddClass “1” to “6” then repeat after six list items jQuery

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #31667
    Steven Gardner
    Participant

    I want to add a left border color on my vertical navigation with each list item is a different colour so i thought about adding a incremented class name that starts to repeat after 6 list items.










  • Can you help me try to accomplish this effect.

    Thanks for any suggestions

    #59673
    Johnnyb
    Member

    I haven’t tested it but something like this should do the trick right?:

     var i = 0;
    $('ul').find('li').each(function(){
    if(i<6){
    i++;
    }
    else{
    i = 1;
    }
    $(this).addClass('class-' + i);
    });

    You might need to tweak it a bit but you get the idea.

    #84335
    Steven Gardner
    Participant

    Cheers perfect!

    #84337
    Mottie
    Member

    Here is another version of Johnnyb’s code (demo):

    $('ul').find('li').each(function(i){
    var num = (i%6) + 1; // mod function - returns the remainder
    $(this)
    .addClass('class-' + num )
    .html('item ' + num);
    });

    #84354
    wolfcry911
    Participant

    you could just use css nth-child to target the list items. of course IE won’t honor that, but give it a fallback color and IE users be damned.

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