How can I hide an item based on the specific class and content with jQuery?

advertisements

Hi there, I would like to hide some elements if a div with class ms-searchref-categoryname text contains Job:

 <div class="ms-searchref-categoryname">Job</div>

 <ul><li></li></ul>

 <a href=""><div class=""></div></a>

How can this be done?


As you said "if div text contains something", you can use this:

if($(".ms-searchref-categoryname:contains('Job')").length > 0) {
   $("#elementToHide").hide();
}

This will hide the element if the string "Job" is found anywhere in the selected element. Here's a working example.

Alternatively, without the use of the :contains pseudo-selector:

if($(".ms-searchref-categoryname").text().indexOf('Job') > -1) {
   $("#hideMe").hide();
}

On the other hand, if you meant you wanted to hide an element if a div only contains a specified string, then @Headshota has given you a working answer already.