views:

596

answers:

4

Hi Guys, I have an HTML table. I need a jQuery selector to select only the TR's, where column2 (TD) text is equal to = "foo". Is this possible?

 <table>
        <tr><td>asdasd</td><td>foo</td><td>fsdf</td></tr>
        <tr><td>asdasd</td><td>xxx</td><td>fsdf</td></tr>
        <tr><td>asdasd</td><td>xxx</td><td>fsdf</td></tr>
        <tr><td>asdasd</td><td>foo</td><td>fsdf</td></tr>
        <tr><td>asdasd</td><td>foo</td><td>fsdf</td></tr>
    </table>

so the selector should return rows 1,4, and 5. Can anyone point me in the right direction?

Thanks a bunch, ~ck in San Diego

+1  A: 
$('tr').filter(function(index){
    return this.cells[1].innerHTML === 'foo';
});
ChaosPandion
A: 

My jQuery may be a little rusty, but does something like this work for you?

$('table tr td:eq(1)').filter(function(index) {
    return $(this).text() === 'foo';
});
Matt Huggins
This will be dramatically slower.
ChaosPandion
A: 

so it might be shortened a little work time:

$("table tr").each(function(){
 if( $(this).text().indexOf("foo")!=-1 ) {
  var el = $('td:eq(1)', $(this));
  if (el.text() == 'foo')
   el.attr("bgcolor", "#336699"); //test
 }
});
andres descalzo
A: 

If 'foo' doesn't have to be in column 2 and just needs to be in the row, you can use this selector as well (I'm just adding this for completeness).

$('tr:contains("foo")').filter(function(index){ return $(this) });
fudgey