Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get click handler from element in JQuery

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.

like image 709
mcottingham Avatar asked Nov 20 '25 10:11

mcottingham


1 Answers

$(...).click is the jQuery click() method, not your event handler.

You can use an undocumented internal API to get the list of event handlers:

jQuery._data( elem, "events" );
like image 57
SLaks Avatar answered Nov 23 '25 01:11

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!