Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addClass every nth

I have a list of elements that I want to style in 3 different ways.

I want every 3rd list item to have the same class throughout the whole list.

For example:

<li class="A">Some Content</li>
<li class="B">Some Content</li>
<li class="C">Some Content</li>
<li class="A">Some Content</li>
<li class="B">Some Content</li>
<li class="C">Some Content</li>
<li class="A">Some Content</li>
<li class="B">Some Content</li>
<li class="C">Some Content</li>

I can do 2 with :odd/even, but how to do it with 3?

like image 578
davebowker Avatar asked Aug 11 '09 13:08

davebowker


1 Answers

try

$("ul li:nth-child(3n+1)").addClass("A")
$("ul li:nth-child(3n+2)").addClass("B")
$("ul li:nth-child(3n)").addClass("C")

Feel free to consolidate it to make it prettier, but I wanted to expose the selectors.

like image 198
Marc Avatar answered Nov 15 '22 23:11

Marc