jQuery | undelegate() with Examples

The undelegate() method is an inbuilt method in jQuery which is used to remove the specified event handler from the selected element.

Syntax:

$(selector).undelegate(selector, event, function)

Parameters: This method accepts three parameters as mentioned above and described below:

  • selector: It is an optional parameter which is used to specify the selector from which event will remove.
  • event: It is an optional parameter which is used to specify the name of the event type on the selector.
  • function: It is an optional parameter which is used to specify the name of the handler function to remove.

Return Value: This method returns the selected element with specified changes made by undelegate() method.

Below examples illustrate the undelegate() method in jQuery:

Example 1: This example does not contain any parameters.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>The undelegate Method</title>
        <script src=
        </script>
          
        <!-- jQuery code to show the working of this method -->
        <script>
            $(document).ready(function() {
                $("body").delegate("p", "click", function() {
                    $(this).css("font-size", "25px");
                });
                $("button").click(function() {
                    $("body").undelegate();
                });
            });
        </script>
        <style>
            div {
                width: 300px;
                height: 100px;
                background-color: lightgreen;
                padding: 20px;
                font-weight: bold;
                font-size: 20px;
                border: 2px solid green;
            }
            button {
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
        <div>
            <!-- click on this p element -->
            <p>Welcome to GeeksforGeeks!.</p>
        </div>
        <!-- click on this button to remove the 
        event handler -->
        <button>Remove...!</button>
    </body>
</html>                                            

chevron_right


Output:
Before click anywhere:

After click on the paragraph:

Note: First click on the button and then click on the paragraph, then no changes occur.

Example 2: This example contains all parameters.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>The undelegate Method</title>
        <script src=
        </script>
          
        <!-- jQuery code to show the working of this method -->
        <script>
            $(document).ready(function() {
                $("body").delegate("div", "click", function() {
                    $(this).animate({
                        height: "+=100px"
                    });
                    $(this).animate({
                        width: "+=100px"
                    });
                });
                $("button").click(function() {
                    $("body").undelegate("div", "click");
                });
            });
        </script>
        <style>
            div {
                width: 30px;
                height: 30px;
                background-color: green;
            }
            button {
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
        <div></div>
        <!-- click on this button -->
        <button>Click here..!</button>
    </body>
</html>

chevron_right


Output:
Before clicking anywhere:

After clicking on the div element get resized.

Note: If click on the button and then click on the div element then no change in the size will take place.



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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.