Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery find text inside <td>

I'm playing around with some selectors, and I'm hitting a wall with selecting Text inside a cell.

Here's a simple attempt I'm making.

<table>
 <tr>
     <td>First Name</td>
     <td>*required</td>
  </tr>
</table> 

I want to change the class for that cell to be "red" - if the string "*required" is found.

Here's my attempt at the jquery:

$("td:contains('*required')").addClass("red");

It's causing all cells to apply that class, it seems. Any better ways to look for specific text?

like image 796
coffeemonitor Avatar asked Nov 01 '10 21:11

coffeemonitor


1 Answers

What you have works, you can test it here, keep in mind that any parent <td> also contains that text though, to do an exact match do this:

$("td").filter(function() { return $.text([this]) == '*required'; })
       .addClass("red");

You can test it here.

like image 105
Nick Craver Avatar answered Oct 04 '22 12:10

Nick Craver