Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How to hide an element that is not contain the specific class

Tags:

jquery

Suppose I have the html code below, I want to ONLY display the "p" element with class="go-1", it is not good to write the statement to hide the element with class="2", class="3", class="100"... how can I write it in a smart way?

The statement can be say as "if "p" class not equal to "go-1" then hide it"
Thanks

<p class="go-1"></p>
<p class="go-2"></p>
<p class="go-3"></p>
...
<p class="go-100"></p>
<p class="go-1"></p>
<p class="go-2"></p>
<p class="go-3"></p>
...
<p class="go-100"></p>
<p class="go-1"></p>
<p class="go-2"></p>
<p class="go-3"></p>
...
<p class="go-100"></p>
like image 842
Charles Yeung Avatar asked Jul 12 '11 04:07

Charles Yeung


People also ask

Which method is used to hide selected elements in jQuery?

The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none.

How do I hide a DOM element?

Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”.

How do I hide a specific div?

We hide the divs by adding a CSS class called hidden to the outer div called . text_container . This will trigger CSS to hide the inner div.


1 Answers

Just use :not():

$('p:not(.go-1)').hide();
like image 120
Blender Avatar answered Oct 01 '22 22:10

Blender