views:

171

answers:

5

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?

+3  A: 

With the `:nth-child´ selector: http://api.jquery.com/nth-child-selector/

$(".stripeMe tr:nth-child(4n), .stripeMe tr:nth-child(4n-1)").addClass("alt");
RoToRa
This doesn't work :) http://jsfiddle.net/ndn67/
Nick Craver
Your example works fine for me...
RoToRa
@RoToRa - Open it in webkit :)
Nick Craver
@Nick: The results at that link look good to me, too.
BlairHippo
@BlairHippo - Don't test in a single browser, try chrome/safari...selectors that are sometimes implemented by the browser and emulated by jquery if not present may not always have the same behavior, which is the case here.
Nick Craver
Correct, it does not work in Chrome, is there a correction for this? thanks
Mark
Well then jQuery is broken there. Another good reason not to use it :-)
RoToRa
@Mark - See my answer for a cross-browser solution
Nick Craver
@RoToRa - I don't agree at all, it's the webkit selector not working, the jQuery emulation (e.g. view it in IE) works fine....write this without jQuery, let me know how verbose that gets :)
Nick Craver
@Nick It's not the selector. Using the selector in CSS works fine in Webkit. http://jsfiddle.net/Pd4Z3/
RoToRa
@RoToRa - Then the problem is still in Sizzle/Webkit :)
Nick Craver
+3  A: 

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).

Nick Craver
This worked for me, thanks very much.
Mark
How can i apply a different css class to the first 2 rows only now? Thanks btw!
Mark
@Mark - If you mean just the **very** first 2, then like this: `$(".stripeMe tr:lt(2)").addClass("otherClass");​​​​`
Nick Craver
A: 

You can use the filter function to filter the set any way you like:

$(".stripeMe tr")
.filter(function(i){ return (i % 4) >= 2; })
.addClass("alt");

This will keep the items with index 2, 3, 6, 7, 10, 11 and so on. Note that the index is zero based, so the third row as index two.

Guffa
A: 

just a fizz buzz like! ;-)

 $('.stripeMe tr').each(function(i) {
    if ( (i % 4) >= 2 ) 
    $(this).addClass('alt');
});

keep: 3,4, 7,8 11,12

aSeptik
A: 

I made a different approach for this problem using a for loop and .eq() method.

var a = 2; // start from 2 because eq() counts from 0
var b = 3; // start from 3 because eq() counts from 0
var total = $('.stripeMe td').length;

for (i = 0; i <= total; i++){
    if (i == a){
        $('.stripeMe tr:eq('+a+')').css('background', 'red');
        a+=4;
    }
    else if (i == b){
        $('.stripeMe tr:eq('+b+')').css('background', 'blue');
        b+=4;
    }
};

fabio