Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector first td of each row

I have this stuff:

<table>   <tr>     <td>nonono</td> <!-- FIND THIS -->     </td>foobar</td>   </tr>   <tr>     <td>nonono2</td> <!-- FIND THIS -->     </td>foobar2</td>   </tr>   <tr>     <td>nonono3</td> <!-- FIND THIS -->     </td>foobar2</td>   </tr> </table> 

I have tried with $('td:first') with no luck;

Expected return: <td>nonono</td>, <td>nonon2</td> and <td>nonon3</td>

Thanks in advance!

like image 300
Ragen Dazs Avatar asked May 21 '13 10:05

Ragen Dazs


People also ask

How can get TD of TR in jquery?

jQuery(". dname"). find("tr td:eq(1)"). val();

How do I get the first row of a table in jquery?

var $row = $(this). closest('table'). children('tbody'). children('tr:first').


2 Answers

You should use :first-child instead of :first:

Sounds like you're wanting to iterate through them. You can do this using .each().

Example:

$('td:first-child').each(function() {     console.log($(this).text()); }); 

Result:

nonono nonono2 nonono3 

Alernatively if you're not wanting to iterate:

$('td:first-child').css('background', '#000'); 

JSFiddle demo.

like image 171
James Donnelly Avatar answered Sep 24 '22 14:09

James Donnelly


try this selector -

$("tr").find("td:first") 

Demo --> http://jsfiddle.net/66HbV/

Or

$("tr td:first-child") 

Demo --> http://jsfiddle.net/66HbV/1/

</td>foobar</td> should be <td>foobar</td>

like image 21
Mohammad Adil Avatar answered Sep 26 '22 14:09

Mohammad Adil