Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 667
Murali VP Avatar asked Mar 07 '11 17:03

Murali VP


People also ask

How do I select a specific column in jQuery?

Columns can be selected using column(). select() or columns(). select() API methods. You can enable single or multiple items selection using select.

Can you select multiple elements in jQuery?

In jQuery, you can select multiple elements by separate it with a comma “,” symbol.

Can you use CSS selectors in jQuery for selecting elements?

Projects In JavaScript & JQueryjQuery uses CSS selector to select elements using CSS. Let us see an example to return a style property on the first matched element. The css( name ) method returns a style property on the first matched element.


2 Answers

$('table[class="services"] tr td:nth-child(3), table[class="services"] tr td:nth-child(2)')
like image 169
Jakub Konecki Avatar answered Oct 27 '22 18:10

Jakub Konecki


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!

like image 20
Will Vousden Avatar answered Oct 27 '22 19:10

Will Vousden