How would I apply the “off” directive to a named handler?
ex
var $btn = $("#theBtn");
var namedHandler = $btn.on("click", function() {
//do something
//then turn off
})
would I turn it off like this
$btn.off("click");
or could I do something else now it is stored in a variable?
namedHandler.off("click");
or
namedHandler.off();
etc.
Any pointers much appreciated.
The same reference to the jQuery object will be in
$btnandnamedHandler. Both return a reference to the same thing so the assignment is assigning the same thing.You could turn it
off()with either method.What may be more suited to your example is namespacing your event, so
off('click', ...)won’t unbind allclickevents.