Razort4x
Razort4x

Reputation: 3406

jQuery pseudo code for selecting nth elelment not working?

I have this table, for which, for each row of it, I want to select the second td. I know the easiest way, (or at least one way) to do this, is use each() and loop for all tr and select/store the second td in some variable, inside the each loop.

But I was thinking if there could be a direct pseudo code that could help me do this direct without any kinda loop, may it be each() or whatever.

When I do this..

$('#ctl00_ContentPlaceHolder1_grdSelectedCloth > tbody > tr > td')

It gives me all td(s) (near about 40, 8 rows, 5 cols each)

when I do this..

$('#ctl00_ContentPlaceHolder1_grdSelectedCloth > tbody > tr > td:odd')

It gives me half td(s) the odd ones,

when I do this

$('#ctl00_ContentPlaceHolder1_grdSelectedCloth > tbody > tr > td:even')

Again, same it gives me 20 td(s), the even one.

but when I do this

$('#ctl00_ContentPlaceHolder1_grdSelectedCloth > tbody > tr > td:2')

It says Syntax error, unrecognized expression: unsupported pseudo: 2

But when I do this..

$('#ctl00_ContentPlaceHolder1_grdSelectedCloth > tbody > tr > td:eq(2)')

It only shows one td??? And just for the first row, I mean, if I apply pseudo code :odd or :even, it gives the td(s) for all rows, but when I do :eq(2) it gives the second td for only the first row??? So, how can I get second td for all rows, preferably by not using each or any kind of loop but by using some sort of pseudo code?

ps: I was running this in firefox console, just in case are wondering what says "Syntax error, unrecognized expression: unsupported pseudo: 2"?

Upvotes: 2

Views: 3445

Answers (3)

Sridhar Narasimhan
Sridhar Narasimhan

Reputation: 2653

$("#ctl00_ContentPlaceHolder1_grdSelectedCloth > tbody > tr > td:nth-child(2)")

Upvotes: 0

palaѕн
palaѕн

Reputation: 73906

Try this:

$('#ctl00_ContentPlaceHolder1_grdSelectedCloth tr td:nth-child(2)');  // would select all cells that are in the 2nd column

Upvotes: 2

zerkms
zerkms

Reputation: 254906

Use :nth-child() instead.

Like td:nth-child(2)

http://jsfiddle.net/zerkms/rFnUL/

Upvotes: 0

Related Questions