Reputation: 68680
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');
Upvotes: 1
Views: 526
Reputation: 81
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:
Upvotes: 1
Reputation: 17984
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.
Upvotes: 0
Reputation: 40507
try
$('table.tst3 tbody tr td:nth-child(2)').addClass('second-col');
Upvotes: -1
Reputation: 47978
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');
Upvotes: 6