Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find first TD with text in a TR with JQuery

I am using JQuery to iterate through all TRs in a table,

but not all rows have values in the first cell.

<TR>
  <TD>3</TD>
  <TD>2</TD>
  <TD>1</TD>
</TR>
  <TD></TD>
  <TD>3</TD>
  <TD>2</TD>
<TR>
</TR>
<TR>
  <TD></TD>
  <TD></TD>
  <TD>3</TD>
</TR>

How can i target the first TD in each row that is not empty and not just the first child?

Thanks!

like image 365
BarakChamo Avatar asked Nov 11 '12 11:11

BarakChamo


1 Answers

Here's a simpler, more elegant solution:

$('tr').find('td:not(:empty):first').css('background', 'red');​

Fiddle: http://jsfiddle.net/dandv/JRcEf/

It just says in jQuery what you mean: "target the first td in each tr that is not empty".

like image 53
Dan Dascalescu Avatar answered Oct 15 '22 03:10

Dan Dascalescu