How can I get a reference to the click handler of an element in JQuery?
Here is what I am trying to do:
Store the click handler, Change the click handler for the next click, Restore the original click handler
var originalClick = $(settings.currentTarget).click;
$(settings.currentTarget).off("click");
$(settings.currentTarget).click(function (e) {
e.preventDefault();
settings.model.modal.close();
$(settings.currentTarget).off("click");
$(settings.currentTarget).click(originalClick);
});
The above code works the first time, however, when I click on the element again it fails:
Uncaught TypeError: Object [object HTMLAnchorElement] has no method 'on'
Update:
I now realize that this is a really bad design that I am trying to do. I have resolved this issue by maintaining a visibility boolean in my viewmodel, if it is true, don't re-open the modal.
$(settings.currentTarget).clickis the function you are binding the event handler with. It is not the event handler itself. How was the original event handler bound? With jQuery or any other way?clickbinding? I don't know how it works internally but I assume knockout is usingaddEventListener. There is no way to get a reference to handlers bound this way.$(settings.currentTarget).off("click");again during the click and never called .On first. It looks like it was already .off as you have at the top.