views:

23

answers:

3
$('table.listings td:contains("You")').each(function(){
 $(this).children('td:nth-child(2)').addClass('highlighted');
});

I have multiple table.listings on the page but the one that contains "You" is selected and I want to addClass highlighted to the 2nd cell in each row, but the above code isn't working as I expected.

Thanks

A: 

Try this:

$('table.listings td:contains("You")').each(function(){
    $("td:nth-child(2)", $(this).parent().parent().get(0)).addClass('highlighted');
});
icktoofay
this will do things which sometimes redundant, `$('table.listings td:contains("You")')` , if the same table contains more `<td>`'s with "You" then the codes inside gets executed more for the same table, doing the same thing.
Reigel
It's worth noting that the person that's asking the question is doing that too, so it's likely that "You" is only in one part of the table, or he/she doesn't care if it's done multiple times.
icktoofay
Yes, but if you see the problem, why not correct it?.. ;)
Reigel
A: 
$('table.listings :contains("You")').each(function(){
 $(this).children('td:nth-child(2)').addClass('highlighted');
});
Reigel
A: 
$('table.listings:contains("You") td:nth-child(2)').addClass("highlight");
gofrwd