views:

80

answers:

4

I want to add the class 'second-col' to all second TD's in each row, but this isn't working:

$('table.tst3 tbody td:eq(1)').addClass('second-col');

Thanks!

A: 

try

$('table.tst3 tbody tr td:nth-child(2)').addClass('second-col');
TheVillageIdiot
This won't work as :eq(1) will reduce the set to only one element. So this code will affect only first row.
RaYell
+5  A: 

use nth-child instead, 'eq' will Reduce the set of matched elements to a single element

$('table.tst3 tbody td:nth-child(2)').addClass('second-col');
najmeddine
A: 

using plain css selection:

    $('table.tst3 tbody td + td').addClass('second-col');

Note that if you have more than 2 columns, the ones after the second column will also be selected. So, depending on your markup this may - or may not be relevant: you would need to overwrite these additional columns with another jquery action (using td + td + td). But i thought i'd point to that possibility.

pixeline
+1  A: 

You can also use:

$('table.tst3 tbody td:odd').addClass('second-col');

or

$('table.tst3 tbody td:even').addClass('second-col');

depending on what you want to select.

documentation:

even

Odd

Jaxwood
That's clever !
pixeline