Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select every other 2 table rows with jquery

I am currently using this code to add a class to every other row in my table.

$(".stripeMe tr:even").addClass("alt");

However, on another table I'd like to add a class to rows 3,4, 7,8, 11,12 and so on...

Is this possible?

like image 432
Mark Avatar asked May 06 '10 15:05

Mark


1 Answers

You need to do it like this:

$(".stripeMe tr:nth-child(4n)").add(".stripeMe tr:nth-child(4n-1)").addClass("alt");​​​​​​​​
//or...
$("tr:nth-child(4n), tr:nth-child(4n-1)", ".stripeMe").addClass("alt");​​​​​​​​​​​​​​​​​

You can see this working here.

Using this:

$(".stripeMe tr:nth-child(4n), .stripeMe tr:nth-child(4n-1)").addClass("alt");​​​​​​​​

gets different results (namely in webkit, possibly others).

like image 165
Nick Craver Avatar answered Sep 23 '22 01:09

Nick Craver