Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery hide() all elements with certain class except one

Tags:

jquery

<div class='hide'>A</div> <div class='hide'>B</div> <div class='hide' id='1'>C</div> 

I have a function called showOne which should hide all elements and then show the one with id='1'.

function showOne(id) { // Hide all elements with class = 'hide' $('#'+id).show(); } 

How do I hide all elements with class = 'hide' in jquery?

like image 308
lisovaccaro Avatar asked May 16 '12 05:05

lisovaccaro


People also ask

What does hide () do in jQuery?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.

What is the opposite of hide in jQuery?

The show() method shows the hidden, selected elements. Note: show() works on elements hidden with jQuery methods and display:none in CSS (but not visibility:hidden). Tip: To hide elements, look at the hide() method.

Which jQuery method is used to hide the selected item?

The hide() is an inbuilt method in jQuery used to hide the selected element. Syntax: $(selector).

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.


2 Answers

Try something like:

function showOne(id) {     $('.hide').not('#' + id).hide(); }  showOne(1);​ 

Demo: http://jsfiddle.net/aymansafadi/kReZn/

I agree with @TheSystemRestart though, "NOTE: DON'T USE ONLY NUMERIC ID".

like image 85
Ayman Safadi Avatar answered Sep 19 '22 13:09

Ayman Safadi


$('div.hide').hide(300,function() {  // first hide all `.hide`    $('#'+ id +'.hide').show(); // then show the element with id `#1` }); 

NOTE: DON'T USE ONLY NUMERIC ID. NOT PERMITTED. READ THIS

like image 39
The System Restart Avatar answered Sep 18 '22 13:09

The System Restart