<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?
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.
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.
The hide() is an inbuilt method in jQuery used to hide the selected element. Syntax: $(selector).
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.
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".
$('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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With