What is the difference between jQuery.show() and jQuery.hide()?


jQuery show() method

The show( speed, [callback] ) method shows all matched elements using a graceful animation and firing an optional callback after completion.

Here is the description of all the parameters used by this method −

  • speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
  • callback − This is optional parameter representing a function to call once the animation is complete.

Example

You can try to run the following code to learn how to work with show() method:

Live Demo

<html>

   <head>
      <title>The jQuery Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
       
      <script>
   
         $(document).ready(function() {

            $("#show").click(function () {
               $(".mydiv").show( 100 );
            });

            $("#hide").click(function () {
               $(".mydiv").hide( 100 );
            });
               
         });

      </script>
       
      <style>
         .mydiv {
             margin:10px;
             padding:12px;
             border:2px solid #666;
             width:100px;
             height:100px;
         }
      </style>
   </head>
   
   <body>
   
      <div class = "mydiv">
         This is a SQUARE.
      </div>

      <input id = "hide" type = "button" value = "Hide" />  
      <input id = "show" type = "button" value = "Show" />  

   </body>
   
</html>

jQuery hide() method

The hide( speed, [callback] ) method hides all matched elements using a graceful animation and firing an optional callback after completion.

Here is the description of all the parameters used by this method −

speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).

callback − This is optional parameter representing a function to call once the animation is complete.

Example

You can try to run the following code to learn how to work with hide() method:

Live Demo

<html>

   <head>
      <title>The jQuery Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
       
      <script>
   
         $(document).ready(function() {

            $("#show").click(function () {
               $(".mydiv").show( 200 );
            });

            $("#hide").click(function () {
               $(".mydiv").hide( 200 );
            });
               
         });

      </script>
       
      <style>
         .mydiv {
             margin:20px;
             padding:20px;
             border:4px solid #666;
             width:100px;
             height:100px;
         }
      </style>
   </head>
   
   <body>
   
      <div class = "mydiv">
         This is a SQUARE.
      </div>

      <input id = "hide" type = "button" value = "Hide" />  
      <input id = "show" type = "button" value = "Show" />  

   </body>
</html>

Updated on: 12-Dec-2019

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements