Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery How to select the 1st cell of each table row

Tags:

jquery

I have a table like this:

<table id="myTable">
   <tr><td>1sta</td><td>2nd</td></tr>
   <tr><td>1stb</td><td>2nd</td></tr>
   <tr><td>1stc</td><td>2nd</td></tr>
   <tr><td>1std</td><td>2nd</td></tr>
</table>

Using jQuery How do I select the 1st <td> element in each row of "myTable"?

like image 642
user169867 Avatar asked Jun 18 '10 18:06

user169867


2 Answers

The first-child selector grabs each td that is the first child of its parent (the tr in this case).

$('#myTable td:first-child');

http://api.jquery.com/first-child-selector/

like image 134
user113716 Avatar answered Sep 20 '22 10:09

user113716


Use this selector '#myTable td:first-child'.

That will select the first td for every tr. Avoid the temptation to use :first instead of :first-child. :first will only select a single element. In this case it would be the first cell of the first row.

http://api.jquery.com/first-child-selector/

like image 25
gestep Avatar answered Sep 21 '22 10:09

gestep