How to hide, show, or toggle your div with jQuery

How to hide, show, or toggle your div with jQuery

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.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
  5. <script>
  6. $(document).ready(function() {
  7. $("#show_menu").click(function () {
  8. $( ".list_container" ).show(3000);
  9. });
  10. $("#hide_menu").click(function () {
  11. $( ".list_container" ).hide(3000);
  12. });
  13. });
  14. </script>
  15. </head>
  16. <body>
  17. <div class="list_container" style="display:none">
  18. <ul>
  19. <li><a href="#">Home</a></li>
  20. <li><a href="#">Tutorials</a></li>
  21. <li><a href="#">Project</a></li>
  22. <li><a href="#">Blog</a></li>
  23. <li><a href="#">Contact</a></li>
  24. </ul>
  25. </div>
  26. <input type="button" class="btn btn-primary" id="show_menu" value="Show Menu">
  27. <input type="button" class="btn btn-primary" id="hide_menu" value="Hide Menu">
  28. </body>
  29. </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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
  5. <script>
  6. $(document).ready(function() {
  7. $("#show_hide").click(function () {
  8. $( ".toggle_container" ).toggle({
  9. duration: 3000,
  10. });
  11. });
  12. });
  13. </script>
  14. </head>
  15. <body>
  16. <div class="toggle_container">
  17. <ul>
  18. <li><a href="#">Home</a></li>
  19. <li><a href="#">Tutorials</a></li>
  20. <li><a href="#">Project</a></li>
  21. <li><a href="#">Blog</a></li>
  22. <li><a href="#">Contact</a></li>
  23. </ul>
  24. </div>
  25. <input type="button" class="btn btn-primary" id="show_hide" value="Show/Hide Menu">
  26. </body>
  27. </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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
  5. <style>
  6. .red{ color:red; }
  7. .green{ color:green; }
  8. </style>
  9. <script>
  10. $(document).ready(function() {
  11. $("#toggle_classes").click(function () {
  12. $( ".toggle" ).toggleClass('red green');
  13. });
  14. });
  15. </script>
  16. </head>
  17. <body>
  18. <div class="toggle_container">
  19. <ul>
  20. <li class="toggle green">Home</li>
  21. <li class="toggle green">Tutorials</li>
  22. <li class="toggle red">Project</li>
  23. <li class="toggle red">Blog</li>
  24. <li class="toggle red">Contact</li>
  25. </ul>
  26. </div>
  27. <input type="button" class="btn btn-primary" id="toggle_classes" value="Toggle Classes">
  28. </body>
  29. </html>

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort - akyazı escort - arifiye escort - erenler escort - eve gelen escort - ferizli escort - geyve escort - hendek escort - karapürçek escort - karasu escort - kaynarca escort - kocaali escort - otele gelen escort - pamukova escort - sapanca escort - söğütlü escort - taraklı escort
sapanca escort hendek escort karasu escort escort sakarya sakarya escort bayan adapazarı escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort