user472285
user472285

Reputation: 2674

How do I hide an element based on specific class and content with jQuery?

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?

Upvotes: 1

Views: 318

Answers (2)

James Allardice
James Allardice

Reputation: 165951

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.

Upvotes: 1

Headshota
Headshota

Reputation: 21449

if($('.ms-searchref-categoryname').html() === 'Job'){
  $('.myElements').hide();
}

Upvotes: 3

Related Questions