views:

45

answers:

2

Hey Guys,

How, using Nth-Child could I add a class to every red box?

alt text

I have tried using:

$('#mainnav li ul li:nth-child(3n)').addClass('yes');

Markup:

<ul>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
      <li><a href="">SUUB</a></li>
</ul>
+3  A: 

This one will put the yes class on all LIs, except for the first one and then every third:

$('#mainnav li ul li:not(:nth-child(3n+1))').addClass('yes');
Gert G
Bingo! gert, thanks a million mate
Keith Donegan
No problem, Keith. :)
Gert G
+1 - Very nice.
patrick dw
Thanks patrick. :)
Gert G
+1  A: 

As one solution, you can apply the class to all items and remove it from every third...

$('#mainnav ul li').addClass('yes').parent().find(':nth-child(3n)').removeClass('yes');​​​​
Andir
This will add the `yes` class to the first two columns.
Gert G
Whoops, good catch ;)
Andir