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.
You can also do this
function handleClick(event) { // code } $('#btn').on('click', handleClick); $('#btn').off('click', handleClick);
Some usefull examples only about on/off here.
The same reference to the jQuery object will be in $btn
and namedHandler
. 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 all click
events.
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