Murali VP
Murali VP

Reputation: 6427

jQuery selector to select 2 and 3rd columns of a table

I am trying to select only 2 and 3 columns of my "services" table.

For example,

$('table[class="services"] tr td:nth-child(3)')

selects the 3rd column, is there a way to select both 2nd and third columns with a single selector?

Upvotes: 5

Views: 15240

Answers (4)

amackay11
amackay11

Reputation: 729

OR... you can do it like this:

$('table.services tr td:nth-child(n+2):nth-child(-n+3)')

Also useful for a range of columns such as columns 4 to 7 (if needed):

$('table.services tr td:nth-child(n+4):nth-child(-n+7)')

Upvotes: 0

Naftali
Naftali

Reputation: 146310

you can do:

$('table.services tr td:nth-child(2), table.services tr td:nth-child(3)')

Upvotes: 1

Will Vousden
Will Vousden

Reputation: 33388

You could split it to avoid repetition of the first part of the selector:

$('table.services tr td').filter(':nth-child(2), :nth-child(3)')

Also note that table.services is the "correct" way to select by class in CSS!

Upvotes: 3

Jakub Konecki
Jakub Konecki

Reputation: 46008

$('table[class="services"] tr td:nth-child(3), table[class="services"] tr td:nth-child(2)')

Upvotes: 10

Related Questions