How to hide div element after few seconds in jQuery ?

Given a div element and the task is to hide the div element after few seconds using jQuery ?

Approach:

  • Select the div element.
  • Use delay function (setTimeOut(), delay()) to provide the delay to hide() the div element.

Example 1: In this example, the setTimeOut() method is used to provide delay to the fadeOut() method.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            How to hide div element after
            few seconds in jQuery ?
        </title>
          
        <style>
            #div {
                background: green;
                height: 100px;
                width: 200px;
                margin: 0 auto;
                color: white;
            }
        </style>
          
        <script src
        </script>
    </head
  
    <body style = "text-align:center;"
          
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style =
            "font-size: 19px; font-weight: bold;">
        </p>
          
        <div id = "div">
            This is Div box.
        </div>
          
        <br>
          
        <button onClick = "GFG_Fun()">
            click here
        </button>
          
        <p id = "GFG_DOWN" style
            "color: green; font-size: 24px; font-weight: bold;">
        </p>
          
        <script>
            $('#GFG_UP').text("Click on button to hide div after 1 sec.");
              
            function GFG_Fun() {
                setTimeout(function() {
                    $('#div').fadeOut('fast');
                }, 1000);
                $('#GFG_DOWN').text("Div hides after 1 second."); 
            }
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: In this example, the delay() method is used to provide delay to the fadeOut() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            How to hide div element after
            few seconds in jQuery ?
        </title>
          
        <style>
            #div {
                background: green;
                height: 100px;
                width: 200px;
                margin: 0 auto;
                color: white;
            }
        </style>
          
        <script src
        </script>
    </head
  
    <body style = "text-align:center;"
          
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style =
            "font-size: 19px; font-weight: bold;">
        </p>
          
        <div id = "div">
            This is Div box.
        </div>
          
        <br>
          
        <button onClick = "GFG_Fun()">
            click here
        </button>
          
        <p id = "GFG_DOWN" style
            "color: green; font-size: 24px; font-weight: bold;">
        </p>
          
        <script>
            $('#GFG_UP').text("Click on button to hide div after 1 sec.");
              
            function GFG_Fun() {
                $("#div").delay(1000).fadeOut(500);
                $('#GFG_DOWN').text("Div hides after 1 second.");     
            }
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.