Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 551
Satch3000 Avatar asked Dec 21 '25 23:12

Satch3000


1 Answers

Try:

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

This will select every 7th element.

See demo on jsFiddle.

like image 126
antyrat Avatar answered Dec 24 '25 15:12

antyrat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!