kovogel
kovogel

Reputation: 1030

Jquery set class to all elements except element with specific class + children/children of children

I want to hide all elements except for an element with a specific class AND all elements inside this.

right now im using

$("body").not(".embedded").hide();

But it also hides the elements inside my .embedded element.

I appreciate every help.

Upvotes: 0

Views: 43

Answers (2)

hungndv
hungndv

Reputation: 2141

Here you are:

$('body :not(".class2")').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="class1">11111</span>
<span class="class2">
    10
    <span class="class21">11</span>
    <span>12</span></span>
<span class="class1">11111</span>

Hope this helps.

Upvotes: 1

AmmarCSE
AmmarCSE

Reputation: 30587

Use * and space to indicate all direct/nested children. Also, for the initial selector, use body * to indicate all children before the not() filtering

$("body *").not(".embedded, .embedded *").hide();

Upvotes: 2

Related Questions