- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
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
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
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.
Cheers perfect!
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);
});
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.