I've an element with two handler bound to it:
<button class="pippo pluto">
push me
</button>
$('.pippo').on('click', function () {
alert("pippo");
});
$('.pluto').on('click', function () {
alert("pluto");
});
I'm trying to .off() only one of them, but the syntax eludes me :-( I'm trying with something among the line of..
<button class="dai">
remove
</button>
$('.dai').on('click', function () {
$('.pippo').off('click');
alert("ok, removed");
});
but this removes both the handler. So I'm trying with...
$('.pippo').off('click .pippo');
but then nothing gets removed.
So I removed the middle space:
$('.pippo').off('click .pippo');
but back to square 1: both handler gets removed.
The right syntax would then be... ?
https://jsfiddle.net/6hm00xxv/
To remove specific delegated event handlers, provide a selector argument. The selector string must exactly match the one passed to . on() when the event handler was attached. To remove all delegated events from an element without removing non-delegated events, use the special value "**" .
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.
jQuery unbind() Method Use the off() method instead. The unbind() method removes event handlers from selected elements. This method can remove all or selected event handlers, or stop specified functions from running when the event occurs. This method can also unbind event handlers using an event object.
unbind( eventType [, handler ] )Returns: jQueryversion deprecated: 3.0. Description: Remove a previously-attached event handler from the elements.
The .off();
method allows you to target multiple selectors as well as a specific event.
$('.pippo').off()
would remove all events for the .pippo
selector.$('.pippo').off('click')
would remove all click
events for the .pippo
selector.$('.pippo').off('click', handler)
would remove all click
events with that handler for the .pippo
selector.In your case the handler
used to add the event listener was an anonymous function so the handler
cannot be used in the off()
method to turn off that event.
That leaves you with three options, either use a variable, use a namespace or both.
Its quite simple to figure out which one to use.
if( "The same handler is needed more than once" ){
// you should assign it to a variable,
} else {
// use an anonymous function.
}
if ( "I intent to turn off the event" && ( "The handler is an anonymous function" || "I want to turn off multiple listeners for this selector at once" ) ){
// use a namespace
}
In your case
So it would look like this
$('.pippo').on('click.group1', function () {
alert("pippo");
});
$('.dai').on('click', function () {
$('.pippo').off('click.group1');
alert("ok, removed");
});
It would work just as well to assign you handler to a variable if you prefer. This allows you to specify which selector, eventType and handler to remove.
var pippo_click = function (e) {
alert("pippo");
});
$('.dai').on('click', function () {
$('.pippo').off('click', pippo_click);
alert("ok, removed");
});
But as a rule you shouldn't create variables if they're not needed.
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