Satch3000
Satch3000

Reputation: 49384

Adding classes to LI with JQuery LI to every 7th item

I am using this code to add a class to every 7th LI items and the first one too:

$('ul li:first, ul li:nth-child(7n)').addClass("first"); 
$('ul li:first, ul li:nth-child(1)').addClass("first"); 

My problem is that it just adds the class to the 1st and the 7th item but if I add another 7 or more it doesn't add it.

I need to add the class the every 7th li item.

Upvotes: 3

Views: 695

Answers (2)

antyrat
antyrat

Reputation: 27765

Try:

$('ul li:nth-child(7n+1)').addClass("first"); 

This will select every 7th element.

See demo on jsFiddle.

Upvotes: 6

Pir Abdul
Pir Abdul

Reputation: 2334

Use Jquery eq

The result of this call id adding a class first to every item 7. Note that the supplied index is zero-based, and refers to the position of the element within the jQuery object, not within the DOM tree.

 $('ul li').eq(6).addClass("first");

Upvotes: 0

Related Questions