Remove the click event for all <p> elements:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/*from www.j a va 2s . c om*/ <script> $(document).ready(function(){ $("p").on("click", function(){ $(this).css("background-color", "red"); }); $("button").click(function(){ $("p").off("click"); }); }); </script> </head> <body> <p>Click this paragraph to change its background color.</p> <button>Remove the click event handler</button> </body> </html>
The off()
method removes event handlers attached with the on()
method.
As of jQuery version 1.7, the off()
method replaces the unbind()
, die()
and undelegate()
methods.
To remove specific event handlers, the selector string must match the one passed to the on()
method.
$(selector).off(event,selector,function(event_Obj),map)
Parameter | Optional | Description |
---|---|---|
event | Required. | one or more events or namespaces to remove from the selected element(s). Multiple event values are separated by a space. |
selector | Optional. | A selector matching the one originally passed to the on() method |
function(event_Obj) | Optional. | function to run when the event occurs |
map | Optional. | an event map ({event:function, event:function, ...}) containing one or more event to attach to the elements, and functions to run when the events occur |