Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting second cell of a row using jQuery

Tags:

jquery

I have the following HTML table

<table>
<tr class="trSelected">
<td>One</td>
<td>two</td>
</tr>
</table>

I just want to get the text in the second column (ie,'two')

I tried

$('.trSelected td').eq(2).val()

but it returs an empty string How it is possible through jQuery?

like image 713
Nithesh Narayanan Avatar asked Jul 19 '11 07:07

Nithesh Narayanan


2 Answers

You should use the following construction:

$('.trSelected td').eq(1).text()

or

$('.trSelected td:eq(1)').text()

eq selector uses zero-based index. Also you have to use text method, not val.

like image 123
Igor Dymov Avatar answered Nov 15 '22 06:11

Igor Dymov


List Indices start from 0, not from 1.

You are trying to select column 3, which does not exist. Also, the .val() method is only used for getting and setting form fields, not normal html elements. Use .text() instead!

$('.trSelected td').eq(1).text()

might work.

like image 29
Powertieke Avatar answered Nov 15 '22 06:11

Powertieke