How to hide, show, or toggle your div with jQuery
In this post we will give you information about How to hide, show, or toggle your div with jQuery. Hear we will give you detail about How to hide, show, or toggle your div with jQueryAnd how to use it also give you demo for it if it is necessary.
It is very easy to set effects in application using jQuery. You can show hide and toggle elements with animation on any event in jQuery.
.show() Method
The show() method display the hidden, selected elements. and it works on element hidden with display:none in CSS and jQuery methods.
Syntax
$(selector).show(speed,callback)
.hide() Method
The hide() method hide the selected elements. and it is similar to display:none in CSS.
Syntax
$(selector).hide(speed,callback)
An example of show and hide menu
In this example we have two button sequentially when i click show menu button then it show menu if hidden and when i click hide menu then it hide menu if shown.
I pass the additional speed parameter in show hide method to run the animation.
- <!DOCTYPEhtml>
- <html>
- <head>
- <scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
- <script>
- $(document).ready(function() {
- $("#show_menu").click(function () {
- $( ".list_container" ).show(3000);
- });
- $("#hide_menu").click(function () {
- $( ".list_container" ).hide(3000);
- });
- });
- </script>
- </head>
- <body>
- <divclass="list_container"style="display:none">
- <ul>
- <li><ahref="#">Home</a></li>
- <li><ahref="#">Tutorials</a></li>
- <li><ahref="#">Project</a></li>
- <li><ahref="#">Blog</a></li>
- <li><ahref="#">Contact</a></li>
- </ul>
- </div>
- <inputtype="button"class="btn btn-primary"id="show_menu"value="Show Menu">
- <inputtype="button"class="btn btn-primary"id="hide_menu"value="Hide Menu">
- </body>
- </html>
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script> <script> $(document).ready(function() { $("#show_menu").click(function () { $( ".list_container" ).show(3000); }); $("#hide_menu").click(function () { $( ".list_container" ).hide(3000); }); }); </script> </head> <body> <div style="display:none"> <ul> <li><a href="#">Home</a></li> <li><a href="#">Tutorials</a></li> <li><a href="#">Project</a></li> <li><a href="#">Blog</a></li> <li><a href="#">Contact</a></li> </ul> </div> <input type="button" id="show_menu" value="Show Menu"> <input type="button" id="hide_menu" value="Hide Menu"> </body> </html>
.toggle() Method
jQuery toggle method is used to display elements if hidden and hide elements if visible.
toggle method is useful when you need to show hide elements on single event.
Syntax
$(selector).toggle(speed,callback)
An example of toggle menu
- <!DOCTYPEhtml>
- <html>
- <head>
- <scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
- <script>
- $(document).ready(function() {
- $("#show_hide").click(function () {
- $( ".toggle_container" ).toggle({
- duration: 3000,
- });
- });
- });
- </script>
- </head>
- <body>
- <divclass="toggle_container">
- <ul>
- <li><ahref="#">Home</a></li>
- <li><ahref="#">Tutorials</a></li>
- <li><ahref="#">Project</a></li>
- <li><ahref="#">Blog</a></li>
- <li><ahref="#">Contact</a></li>
- </ul>
- </div>
- <inputtype="button"class="btn btn-primary"id="show_hide"value="Show/Hide Menu">
- </body>
- </html>
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script> <script> $(document).ready(function() { $("#show_hide").click(function () { $( ".toggle_container" ).toggle({ duration: 3000, }); }); }); </script> </head> <body> <div > <ul> <li><a href="#">Home</a></li> <li><a href="#">Tutorials</a></li> <li><a href="#">Project</a></li> <li><a href="#">Blog</a></li> <li><a href="#">Contact</a></li> </ul> </div> <input type="button" id="show_hide" value="Show/Hide Menu"> </body> </html>
.toggleClass() Method
jQuery toggleClass method is used to add remove one or more class names to selected elements.
jQuery toggleClass method first check each elements for class name if class names are missing then it add the class name and if class names are added already then it remove the class name to selected elements.
Syntax
$(selector).toggleClass(classname)
An example of toggle menu
- <!DOCTYPEhtml>
- <html>
- <head>
- <scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
- <style>
- .red{ color:red; }
- .green{ color:green; }
- </style>
- <script>
- $(document).ready(function() {
- $("#toggle_classes").click(function () {
- $( ".toggle" ).toggleClass('red green');
- });
- });
- </script>
- </head>
- <body>
- <divclass="toggle_container">
- <ul>
- <liclass="toggle green">Home</li>
- <liclass="toggle green">Tutorials</li>
- <liclass="toggle red">Project</li>
- <liclass="toggle red">Blog</li>
- <liclass="toggle red">Contact</li>
- </ul>
- </div>
- <inputtype="button"class="btn btn-primary"id="toggle_classes"value="Toggle Classes">
- </body>
- </html>
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script> <style> .red{ color:red; } .green{ color:green; } </style> <script> $(document).ready(function() { $("#toggle_classes").click(function () { $( ".toggle" ).toggleClass('red green'); }); }); </script> </head> <body> <div > <ul> <li >Home</li> <li >Tutorials</li> <li >Project</li> <li >Blog</li> <li >Contact</li> </ul> </div> <input type="button" id="toggle_classes" value="Toggle Classes"> </body> </html>
Hope this code and post will helped you for implement How to hide, show, or toggle your div with jQuery. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs