i want to break out each three items in a list and add a class to that child like;
<ul>
<li>1</li>
<li>2</li>
<li>3</li><!--target list item-->
<li>4</li>
<li>5</li>
<li>6</li><!--target list item-->
<li>7</li>
</ul>
any idea?
You should use the nth-child pseudo selector
$("ul li:nth-child(3n)").addClass("break-here");
There's a CSS pseudo selector for that:
:nth-child(xn+y)
selects every x
child starting at y
, so in your case x = 3
and y = 1
(the default)
$('li:nth-child(3n)').addClass(...);
Demo at http://jsfiddle.net/8WDK4/
See http://www.w3.org/TR/selectors/#nth-child-pseudo and examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With