$('.table tbody td:eq(3)').addClass('col4');
..works, but only selects the first cell, not all cells in the column.
$('.table tbody td:eq(3)').addClass('col4');
..works, but only selects the first cell, not all cells in the column.
Try:
$(".table tbody td:nth-child(4)").addClass("col4");
Note: I've put 4 here because :eq(n)
is zero-based and :nth-child(n)
is one-based.
You've stumbled on the key difference between the two. eq(3)
will return exactly one element, the 4th from the entire set. Much like how :first
only returns one element (max) whereas :first-child
can return many.
You could also use the <col>
element to style whole columns instead of having to apply styles to all <td>
s in that column.