The
off() Method in jQuery is used to remove event handlers attached with the on() method. The off() method brings a lot of consistency to the API and it replace unbind(), die() and undelegate() methods.
Syntax:
$(selector).off(event, selector, function(eventObj), map)
Parameter: This method accepts four parameters as mentioned above and described below:
- event: It is required parameter and used to specify one or more events or namespaces to remove from the selected elements. Multiple events are separated by space.
- selector: It is optional parameter and used to match with the one originally passed to the on() method when attaching event handlers
- function(eventObj): It is optional parameter and used to specify the function to run when the event occurs.
- map: This parameter is used to specify the event map ({event:function, event:function, ...}) which contains one or more event attach to the elements, and functions to run when the events occur.
Example 1: This example removes the event handler.
Output:
Before Click on the element h3:
👁 Image
After Click on the element h3:
👁 Image
Example 2: This example use animate event to add animation effect one time and then remove the event handler.
Output :
Before Click on the heading:
👁 Image
After Click on the heading:
👁 Image