Wilson
Wilson

Reputation: 8768

Getting third cell of each row

var Rows = $("#tableid tbody tr");
Rows.each(function(index, element) {
var thirdCell = $(this+":nth-child(3)").text();
    alert(thirdCell);
});

Something in the var thirdCell line is not working correctly. Each row has four children, all td tags. I want to get the text inside the third cell.

Upvotes: 2

Views: 1939

Answers (4)

egbrad
egbrad

Reputation: 2417

this is not a string, it's a jquery object, so it can't be appended to when you are building your new selector. You could do it like this:

var selector = $(this).selector;
var thirdCell = $(selector+":nth-child(3)").text();
alert(thirdCell);

http://jsfiddle.net/2URV7/

Upvotes: 1

gray state is coming
gray state is coming

Reputation: 2107

you can get all cells from each row by 0 based index

Rows.each(function(index, element) {
    var thirdCell = $(this.cells[2]).text();
    alert(thirdCell);
});

or you can get it in original selector

var Third = $("#tableid tbody tr > td:nth-child(3)");

Third.each(function(index, element) {
    var thirdCell = $(this).text();
    alert(thirdCell);
});

Upvotes: 0

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79840

Try something like below,

var Rows = $("#tableid tbody tr");
Rows.each(function(index, element) {
var thirdCell = $(this).find('td').eq(2).text();
    alert(thirdCell);
});

Upvotes: 5

Anonymous
Anonymous

Reputation: 4630

This will not work because tr has no text. it has TDs. you can apply though html() to get the innerHTML of the tr, or use text() on tds if they actually contain text.

Upvotes: 0

Related Questions