0

I have this script:

$(document).ready(function(){
    var search = 'foo';
    $("table tr td").filter(function() {
        return $(this).text() == search;
    }).parent('tr').css('background-color','#f2dede');
});

What I want it to do is to color the background of the table whether the text is "foo" or "supfoo" or "foobar" or "foo foo". Anything with "foo" in it should be colored pink.

Not sure what to change! Please help...

3
  • A regex is probably better here than == Commented Jan 12, 2017 at 17:54
  • 1
    I'd probably just use $(this).text().indexOf('foo') > -1 for this. Commented Jan 12, 2017 at 17:56
  • Thanks, nurdyguy! That worked just as I wanted. Commented Jan 12, 2017 at 18:39

1 Answer 1

2

Could you use :contains()?

$( "tr:contains('foo')" ).css( "background-color", "red" );
Sign up to request clarification or add additional context in comments.

3 Comments

You'd want to be careful that you aren't overly broad. This checks every tr. But yes, this general approach could work well.
@nurdguy, I was banking on him knowing what he was doing. The slightest inkling of jQuery knowledge tells you this selects every TR.
I agree. I just wanted to give him a heads up before he pastes the code in somewhere and all heck breaks loose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.