Home > Mobile >  How to apply jQuery rule except if it is inside an element with certain class?
How to apply jQuery rule except if it is inside an element with certain class?

Time:07-21

Sample HTML code:

<div  ...>
<div  .....>
<figure>. first figure....</figure>
</div>
</div>
<figure>. second figure....</figure>

Now I want to remove all elements which are NOT (!) inside an element with a class=foobar

If I code this with jQuery:

$("figure").remove();

then ALL elements are removed.

How can I restrict the removal to only elements OUTSIDE of element with a class=foobar?

In the sample code above only the second figure should be removed

Be aware the element with class=foobar need not to be the direct parent element of

CodePudding user response:

You just need to update your selector.

   $("figure:not(.foobar figure)").remove();
  • Related