If div contains word "example" , then addClass display none to another div

contains, if-statement, jquery

Solution

You need to check the `length` of the returned jQuery object:

if (jQuery("div.contactUs:contains('contact')").length) {
    jQuery(".hideThis").css("display","none");
}

The reason for this is the jQuery returns an object, even if no matching elements are found, and that will never evaluate to `false`.

Also note that you can use `hide` instead of `css`, just to make your code a little bit shorter:

if (jQuery("div.contactUs:contains('contact')").length) {
    jQuery(".hideThis").hide();
}

Problem

I am trying to hide a `div`, if a specific word is inside another `div`. ``` if (jQuery("div.contactUs:contains('contact')")) { jQuery(".hideThis").css("display","none"); } ``` But it does not seem to work. Any ideas ?

Original source