user169867
user169867

Reputation: 5870

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

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"?

Upvotes: 6

Views: 1493

Answers (2)

gestep
gestep

Reputation: 21

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/

Upvotes: 2

user113716
user113716

Reputation: 322492

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/

Upvotes: 8

Related Questions