deadbug
deadbug

Reputation: 444

Jquery: Doing a search on second column of a table

With reference to this answer for live search through table rows, the answer illustrates how to search through first column with the following statement:

var id = $row.find("td:first").text();

My requirement is to search through second column. Replacing first with second is not working.

Upvotes: 0

Views: 1637

Answers (2)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

You can use eq selector:

var id = $row.find("td:eq(1)").text();

Index starting from 0.


You may also use css selector :nth-child instead. There's no :second or like :third selectors in css. The css selector only accepts :first-child, :last-child selectors and the jQuery utilize them using shorthand :first and the :last selector. So, you can use :first, :last, :first-child, :last-child in jQuery selector.

var id = $row.find("td:nth-child(2)").text();

The :nth-child index starts with 1.

Upvotes: 3

xhg
xhg

Reputation: 1885

Another way to search through cells of 2nd column is to use nth-of-type() CSS selector.

$('td:nth-of-type(2)').each(function(index){
    $(this).text();
})

Upvotes: 0

Related Questions