The Wayback Machine - https://web.archive.org/web/20250402193344/https://www.geeksforgeeks.org/jquery-hide-method/
Open In App

jQuery hide() Method

Last Updated : 17 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The jQuery hide() method is used to hide selected HTML elements with a smooth animation effect. It reduces the element’s height to zero, effectively making it invisible. The method can also take a duration parameter to control the speed of the hiding animation.

Syntax:

$(element_selector).hide(duration, easing, call_function);

Parameter: It accepts three parameters which are specified below-

  • duration: It specifies the speed of the hide effect.
  • easing: It specifies the speed of the element at different points of animation.
  • call_function: This is the call-back function to be executed after the hide operation.

Return Value: It does not return any value.

Example 1: In the we use jQuery to hide a paragraph when a button is clicked. It also styles a div with a green border, padding, and adjusts the button’s margin for spacing.

HTML
<!DOCTYPE html>
<html>

<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $(".b1").click(function () {
                $("p").hide();
            });
        });
    </script>
    <style>
        div {
            width: 50%;
            height: 80px;
            padding: 20px;
            margin: 10px;
            border: 2px solid green;
            font-size: 30px;
        }

        .b1 {
            margin: 10px;
        }
    </style>
</head>

<body>
    <div>
        <p>GeeksforGeeks !.</p>
    </div>
    <!-- click on this button and 
        above paragraph will disappear -->
    <button class="b1">Click me !</button>

</body>

</html>

Output: 

Example 2: In the below example, we use jQuery to hide a paragraph when a button is clicked. The paragraph hides over 1000 milliseconds, and an alert message appears once the hide animation is complete.

HTML
<!DOCTYPE html>
<html>

<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <!--jQuery code to show the working of this method-->
    <script>
            $(document).ready(function () {
                $(".btn1").click(function () {
                    $("p").hide(1000, function () {
                        alert("Hide() method has finished its working!");
                    });
                });
            });
    </script>
    <style>
        p {
            width: 40%;
            padding: 20px;
            height: 50px;
            border: 2px solid green;
        }
    </style>
</head>

<body>
    <p>GeeksforGeeks.!</p>
    <!-- click on this button and 
        above paragraph will hide -->
    <button class="btn1">Click to Hide</button>
</body>

</html>

Output: 

jQuery hide() Method – FAQs

Can the jQuery hide() method accept a callback function?

Yes, you can pass a callback function to execute after the hide animation completes.

What parameters does the jQuery hide() method accept?

It accepts duration, easing, and a callback function.

How can I hide multiple elements with jQuery hide()?

Select multiple elements using a common selector and apply hide(), e.g., $(“p, .class”).hide();.

Is there a way to hide elements instantly with jQuery hide()?

Yes, use hide(0) or simply hide() without specifying a duration for an instant effect.

Can I use jQuery hide() to hide elements conditionally?

Yes, apply hide() within a conditional block or based on events, like button clicks or specific conditions.



Next Article

Similar Reads

three90RightbarBannerImg