1

Suppose to have this html code:

<tr id="row_1">
 <td>1</td>
 <td>Text</tr>
</tr>

<tr id="row_2">
 <td>2</td>
 <td>home</tr>
</tr>

<tr id="row_n">
 <td>n</td>
 <td>n row</tr>
</tr>

I need to take only the first td element in a row, so I do:

$('[id^="row_"] > td ').each(function(){

});

But this jquery code it doesn't work because it gets me all first td children of tr but I want only the first td child of tr. Anyone can help?

Polly
  • 637
  • 3
  • 12
  • 25

3 Answers3

2

Maybe with this:

$('[id^="row_"] > td:first-child')
amedina
  • 2,838
  • 3
  • 19
  • 40
1

You should use :first-child

You can do this using .each().

Example:

$('td:first-child').each(function() {
    console.log($(this).text());
});
Jari Rengeling
  • 326
  • 1
  • 15
0

you can use :first-child like this:

var firstTd = $('td:first-child');

ref: https://api.jquery.com/first-child-selector/

treyBake
  • 6,440
  • 6
  • 26
  • 57