jQuery - off( events [, selector ] [, handler(eventObject) ] ) Method



Description

The off( events [, selector ] [, handler(eventObject) ] ) method does the opposite of on() method, it removes a bound live event.

Syntax

Here is the simple syntax to use this method −

selector.on( event, selector, handler ) 

Parameters

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

  • events − Event types separated by spaces.

  • selector − A Selector String

  • handler − A function to bind to the event on each of the set of matched elements

Example

Following is a simple example a simple showing the usage of this method.

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://www.tutorialspoint.com/jquery/jquery-3.6.0.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {

            function aClick() {
               $("div").show().fadeOut("slow");
            }
				
            $("#bind").click(function () {
               $("#theone").on("click", aClick).text("Can Click!");
            });
				
            $("#unbind").click(function () {
               $("#theone").off("click", aClick).text("Does nothing...");
            });
				
         });
      </script>
		
      <style>
         button { margin:5px; }
         button#theone { color:red; background:yellow; }
      </style>
   </head>
	
   <body>
	
      <button id = "theone">Does nothing...</button>
      <button id = "bind">Bind Click</button>
      <button id = "unbind">Unbind Click</button>
		
      <div style = "display:none;">Click!</div>
   </body>
</html>

This will produce following result −

jquery-events.htm
Advertisements