Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding class as per LI postion [duplicate]

I have below markup,

<ul>
<li>1</li> //light BG
<li>2</li>
<li>3</li>
<li>4</li> //light BG
<li>5</li>
<li>6</li>
......
</ul>

I want to add class dark and light, as per position of li.

  • First li should be light
  • 2nd,3rd should be dark
  • 4th, 5th should be light
  • same pattern....
like image 840
ashokcc Avatar asked Jan 19 '26 01:01

ashokcc


1 Answers

$('ul li:nth-child(3n-2)').addClass('light');

$('ul li::not(:nth-child(3n-2))').addClass('dark');

DEMO

like image 76
thecodeparadox Avatar answered Jan 20 '26 15:01

thecodeparadox



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!