Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 402
jwaliszko Avatar asked Jun 16 '10 10:06

jwaliszko


People also ask

How to get datatable cell value in jQuery?

jQuery: code to get TD text value on button click. text() method we get the TD value (table cell value). So our code to get table td text value looks like as written below. $(document). ready(function(){ // code to read selected table row cell data (values).

How to get table tr td id in jQuery?

$(function() { var bid, trid; $('#test tr'). click(function() { trid = $(this). attr('id'); // table row ID alert(trid); }); });

How to get current tr td value in jQuery?

$(this). closest('tr'). children('td:eq(0)'). text();


2 Answers

Yes:

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

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

like image 69
Philippe Leybaert Avatar answered Sep 28 '22 04:09

Philippe Leybaert


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]; 
like image 28
Tim Down Avatar answered Sep 28 '22 04:09

Tim Down