Jquery Hide,Show, Toggle Effects – Tutorial

jQuery hide() Method

The jQuery if you want to hide the any content or any think then use method

Syntax:

$(selector).hide();

$(selector).hide(speed, callback);

$(selector).hide(speed, easing, callback);

speed: It is an optional parameter. It specifies the speed of the delay. Its possible vales are slow, fast and milliseconds.

  easing: It specifies the easing function to be used for transition.

  callback: It is also an optional parameter. It specifies the function to be called after completion of hide() effect.

<!DOCTYPE html>

  <html>

  <head>

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">             </script>

   <script>

    $(document).ready(function(){

    $("p").click(function(){

    $(this).hide();

      });

   });

 </script>

 </head>

 <body>

 <p>If you click on me, I will disappear.</p>

 <p>Click me away!</p>

 <p>Click me too!</p>

</body>

</html>

Jquery hide effects with speed

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of jQuery Animated Show Hide Effects</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<style>
    p{
        padding: 15px;
        background: #F0E68C;
    }
</style>
<script>
$(document).ready(function(){
    // Hide displayed paragraphs with different speeds
    $(".hide-btn").click(function(){
        $("p.normal").hide();
        $("p.fast").hide("fast");
        $("p.slow").hide("slow");
        $("p.very-fast").hide(50);
        $("p.very-slow").hide(2000);
    });
});
</script>
</head>
<body>
    <button type="button" class="hide-btn">Hide Paragraphs</button> 
    <p class="very-fast">This paragraph will show/hide with very fast speed.</p>
    <p class="normal">This paragraph will show/hide with default speed.</p>
    <p class="fast">This paragraph will show/hide with fast speed.</p>
    <p class="slow">This paragraph will show/hide with slow speed.</p>
    <p class="very-slow">This paragraph will show/hide with very slow speed.</p>
</body>
</html>

jQuery show() Method

The jQuery show() method is used to show the selected elements. 

   
  Syntax:

        $(selector).show();  

        $(selector).show(speed, callback);  

        $(selector).show(speed, easing, callback);  

Example

<!DOCTYPE html>
   <html>
   <head>
    <script                                                    src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
     $(document).ready(function(){
      $("#show").click(function(){
      $("p").show();
          });
      });
   </script>
   </head>
   <body>
     <p>If you click on the "Hide" button, I will disappear.</p>
  <button id="show">Show</button>
  </body>
  </html>



 Jquery hide effects with speed Example 

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of jQuery Animated Show Hide Effects</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<style>
    p{
        padding: 15px;
        background: #F0E68C;
    }
</style>
<script>
$(document).ready(function(){  
    // Show hidden paragraphs with different speeds
    $(".show-btn").click(function(){
        $("p.normal").show();
        $("p.fast").show("fast");
        $("p.slow").show("slow");
        $("p.very-fast").show(50);
        $("p.very-slow").show(2000);
    });
});
</script>
</head>
<body>
    <button type="button" class="show-btn">Show Paragraphs</button>
    <p class="very-fast">This paragraph will show/hide with very fast speed.</p>
    <p class="normal">This paragraph will show/hide with default speed.</p>
    <p class="fast">This paragraph will show/hide with fast speed.</p>
    <p class="slow">This paragraph will show/hide with slow speed.</p>
    <p class="very-slow">This paragraph will show/hide with very slow speed.</p>
</body>
</html>

Jquery hide effects with speed Example

jQuery Toggle() Method

You can also toggle between hiding and showing an element with the toggel() method.

Syntax

$(selector).toggle(speed,callback);

Example:-

 <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Example of jQuery Toggle Effect</title>
     <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
     <style>
       p{
           padding: 15px;
           background: #F0E68C;
        }
     </style>
     <script>
      $(document).ready(function(){
        // Toggles paragraphs display
        $(".toggle-btn").click(function(){
          $("p").toggle();
       });
    });
    </script>
    </head>
     <body>
      <button type="button" class="toggle-btn">Toggle Paragraphs</button> 
      <p>This is a paragraph.</p>
      <p>This is another paragraph.</p>
     </body>
  </html> 

Jquery Toggle effects with speed Example

<script>
    $(document).ready(function(){
    // Toggles paragraphs with different speeds
    $(".toggle-btn").click(function(){
        $("p.normal").toggle();
        $("p.fast").toggle("fast");
        $("p.slow").toggle("slow");
        $("p.very-fast").toggle(50);
        $("p.very-slow").toggle(2000);
    });
});
</script>

You can also see the example of callback function in jquery toggel() method

Example

<script>
$(document).ready(function(){
    // Display alert message after toggling paragraphs
    $(".toggle-btn").click(function(){
        $("p").toggle(1000, function(){
            // Code to be executed
            alert("The toggle effect is completed.");
        });
    });
});
</script>

Leave a Comment

Show Buttons
Hide Buttons