eozzy
eozzy

Reputation: 68680

Adding class to all elements using :eq

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

Answers (4)

Jaxwood
Jaxwood

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:

even

Odd

Upvotes: 1

pixeline
pixeline

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

TheVillageIdiot
TheVillageIdiot

Reputation: 40507

try

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

Upvotes: -1

manji
manji

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

Related Questions