

How to Remove an Event Handler in jQuery
source link: https://www.laravelcode.com/post/how-to-remove-an-event-handler-in-jquery
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Remove an Event Handler in jQuery
Use the jQuery off()
Method
You can simply use the jQuery off()
method to remove the event handlers that were attached with on()
. Don't use the unbind()
method, it has been deprecated since jQuery 3.0.
If you try out the following example, you'll find the event handler function sayHello()
is not executing for the button element having the id btnOff
:
<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Remove Event Handler</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
function sayHello(){
alert("Hello, World!");
}
$(document).ready(function(){
// Attach an event handler function to click event
$("#btnOn, #btnOff").on("click", sayHello);
// Removes the click event handler from second button
$("#btnOff").off("click");
});
</script>
</head>
<body>
<button type="button" id="btnOn">Click On</button>
<button type="button" id="btnOff">Click Off</button>
</body>
</html>
Author : Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Recommend
-
56
Plugins: Util ( #23565 ) Alert ( #23596 )
-
13
Everything is terrible (but more so inside a keypress event handler)Everything is terrible (but more so inside a keypress event handler) 22 Jan 2019 Happy new years. We got really, really close to being able to sh...
-
14
Add Remove Input Fields Dynamically using jQuery Dynamic input field allows providing multiple values in a form. It is very useful when you want to receive multiple inputs for the same field in an HTML form...
-
10
Bridge jQuery UI droppable for Lift ajax handler advertisements I'm working on a web application project using Scala / Lift. I would like to m...
-
5
Event handler for multiple events I’m not sure that everybody is aware that a single event handler method (in Dynamics 365 for Finance and Operations) can be decorated with several event handler attributes.
-
4
Set onclick property import "github.com/gopherjs/gopherjs/js" element := js.Global.Get("document").Call("getElementById", "foo")...
-
7
[JavaScript] Firing Order (Precedence) of Event Handler Updated: March 03, 20...
-
12
[JavaScript] Scope (Context) of Event Handler Function Updated: March 07, 2015...
-
4
When checking if an event handler is null, is this done on a per-thread basis? Ensuring someone is listening to the event is done like this:...
-
1
Protecting a broker from a failing event handler Raymond Che...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK