nagyben
nagyben

Reputation: 938

jQuery .off() not removing handlers

I have a dynamically loaded button created when the document loads. After this, I attach a click handler to this button. When I try to remove the handler with .off(), it does not work.

This snippet creates the click handler and removes it. However, when I click the button, the function StartMash still executes.

$(document).on("click", "#mash-idle-start", StartMash);
$("#mash-idle-start").off();

Obviously this is not the functionality I am trying to achieve, but the problem persists through this simple example

Upvotes: 0

Views: 1320

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

because the event handler is not attached to #mash-idle-start, it is attached to document so

$(document).off("click", "#mash-idle-start", StartMash);

or

$(document).off("click", "#mash-idle-start");

Note: When working with event unbinding, try to use event namespaces as you will have more control over which handlers are removed.

Upvotes: 5

Related Questions