daniel.tosaba
daniel.tosaba

Reputation: 2563

hide div that contains <p> that contains "text" - how to

<div id="example">
 <ul>
  <p>text</p>
 </ul>
<div>

i need to hide div itself

Upvotes: 2

Views: 3416

Answers (3)

Peeter
Peeter

Reputation: 9382

$("div p:contains('text')").closest("div").hide();

Working solution: http://jsfiddle.net/7qmwj/8/

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385284

The solutions with parent() can't guarantee that the right parent is being used.

Use this:

$('div').has("p:contains('text')").hide();

Upvotes: 1

Russ Cam
Russ Cam

Reputation: 125528

Original Answer:

$("div p:contains('text'))").hide();

This won't work because it hides the <p> and not the <div>. We also need to employ :has.

$("div:has(p:contains('text'))").hide();

Demo here

Upvotes: 8

Related Questions