Now to remove the click event handler from the click event of the button, just use the removeEventListener() event handler as follows: btn. removeEventListener('click', handler); Note that the event name and the event handler function must be the same for removeEventListener() to work.
You can remove all event listeners from a DOM element in Javascript by replacing the element with a deep clone of itself. elem. cloneNode(...) will not clone the event listeners of the source element.
The removeEventListener() method removes an event handler from an element.
The . off() method removes event handlers that were attached with .
You would use off() to remove an event like so:
$("#saveBtn").off("click");
but this will remove all click events bound to this element. If the function with SaveQuestion is the only event bound then the above will do it. If not do the following:
$("#saveBtn").off("click").click(function() { saveQuestion(id); });
Is there a way to remove all previous click events that have been assigned to a button?
$('#saveBtn').unbind('click').click(function(){saveQuestion(id)});
$('#saveBtn').off('click').click(function(){saveQuestion(id)});
If you used...
$(function(){
function myFunc() {
// ... do something ...
};
$('#saveBtn').click(myFunc);
});
... then it will be easier to unbind later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With