jwaliszko
jwaliszko

Reputation: 17064

How to get table cell at specified index using jQuery

I know that I can get first or last table cell (e.g. for last row) using jQuery expression like below:

first cell: $('#table tr:last td:first') or last cell: $('#table tr:last td:last')

But, how can I get cell at specific index, for example index 2, using similar expression, i.e. something like $('#table tr:last td:[2]') ?

Regards.

Upvotes: 28

Views: 54205

Answers (3)

Tim Down
Tim Down

Reputation: 324567

It's very simple without jQuery, and will work faster and in more browsers:

var table = document.getElementById("table");
var row = table.rows[table.rows.length - 1];
var cell = row.cells[2];

Upvotes: 18

Blair Conrad
Blair Conrad

Reputation: 241790

Use the nth-child selector.

For example,

$('#table tr:last td:nth-child(2)')

Upvotes: 6

Philippe Leybaert
Philippe Leybaert

Reputation: 171774

Yes:

$('#table tr:last td:eq(1)')

that will give you the second td in the last row.

Upvotes: 41

Related Questions