1

I have this table

<table> <tr> <td>Name</td> <td>John Stevenson</td> </tr> <tr> <td>Name</td> <td>John Stevenson</td> </tr> <tr> <td>Name</td> <td>Jack Stevenson</td> </tr> </table> 

How can I find if there is "Jack Stevenson" and if it true, set this td color to red.

My jQuery code not works:

$("table").find("td").each(function () { var hiImJohn = $("td:contains('John Stevenson')"); var td = $(this).text(); if ( td.indexOf("John Stevenson") > 0 ) { hiImJohn.css({color: "red"}); } }); 

2 Answers 2

3

Try following

$("table").find("td:contains('John Stevenson')").each(function () { $(this).css({color: "red"}); }); 

For reference - http://plnkr.co/edit/q45cGWpW8ql7FfoNaKDj?p=preview

EDIT

Please note, there is no need of each loop either. Updated code will be as follows

$("table").find("td:contains('John Stevenson')").css({color: "red"}); 
Sign up to request clarification or add additional context in comments.

4 Comments

There is no need of each() you can just use $("table").find("td:contains('John Stevenson')").css({color: "red"})
@Satpal - Thanks. I missed to update that while copy pasting it. Glad you noticed. Will update
If you're micro-optimising, then there's also no need for .find either ... :)
@freedomn-m - Correct. However, I will like to stick to what I have written above. Thanks :)
1

Try this

$("table").find("td").each(function () { if($(this).text() == "John Stevenson"){ $(this).css("background-color:red"); } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.