Traversing a table column -jQuery
Asked Answered
I

2

5

Given a 3 by 3 table, i want to add a class to all the cells of 3rd column .

I have tried doing

$( 'td:eq(3)' ).addclass('special');
$( 'td:eq(5)' ).addclass('special');
$( 'td:eq(8)' ).addclass('special');

but the problem is writing 3 lines of code. Can a single line of code do it ?

Incandesce answered 11/2, 2012 at 15:50 Comment(0)
E
2
$("td:nth-child(3)").addClass('special');

good article about nth-child -

http://css-tricks.com/how-nth-child-works/

Encroach answered 11/2, 2012 at 15:57 Comment(1)
I should have left the tr > part off of my answer since it really isn't necessary. +1Dominate
D
7
$('tr > td:nth-child(3)').addClass('special');

DEMO: http://jsfiddle.net/TcQex/

DOCS: http://api.jquery.com/nth-child-selector

Dominate answered 11/2, 2012 at 15:54 Comment(0)
E
2
$("td:nth-child(3)").addClass('special');

good article about nth-child -

http://css-tricks.com/how-nth-child-works/

Encroach answered 11/2, 2012 at 15:57 Comment(1)
I should have left the tr > part off of my answer since it really isn't necessary. +1Dominate

© 2022 - 2024 — McMap. All rights reserved.