Zak
Zak

Reputation: 2698

Can you use jquery's :eq() selector twice in the same expression?

I'm trying to write a table sorter function, so I need to be able to get the HTML of the nth column of the nth row of the table. I tried the following, and it didn't work (it returns null, and that specific column definitely has text inside of it):

$('tr:eq(3) td(eq(2)').html()

But later in the code, when I use this, it works fine:

$('tr:eq(3)').attr('id')

Is it not possible to use :eq() twice? If it is, am I calling it wrong? If not, is there a way I can do this another way?

Upvotes: 0

Views: 690

Answers (2)

user113716
user113716

Reputation: 322522

@BoltClock has the jQuery fix for you in his answer.

FYI, you can access row/column easily using native methods.

From the table element, just do .rows[3].cells[2]. This will be a very fast way to do a cell lookup.

var myTable = $('#theTable')[0];

var html = myTable.rows[3].cells[2].innerHTML;

Upvotes: 4

BoltClock
BoltClock

Reputation: 724074

Well you have a ( instead of a : in here:

td(eq(2)

An alternative is :nth-child() which uses 1-based indexing instead of 0-based:

$('tr:nth-child(4) td:nth-child(3)').html()

Upvotes: 3

Related Questions