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
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort