Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable button click handlers in Javascript

Ok, let's say that I have a button and I would like to enable/disable its click handlers.

I would like to create functions like these:

var bar = [];
function storeHandlers(buttonID) {
    //I would like to store the click handlers in bar[buttonID]
}

function preventHandlers(buttonID) {
    storeHandlers(buttonID);
    //I would like to disable all the click handlers for button
}

function loadHandlers(buttonID) {
    //I would like to enable all the handlers loaded from bar[buttonID]
}

Is this possible? How can I achieve this?

Thank you in advance

like image 241
Lajos Arpad Avatar asked Jan 24 '26 10:01

Lajos Arpad


1 Answers

As far as I know you can't store/retrieve the event handlers from just the ID of the elements, you have store those manually while creating handlers. But you can on/off handlers like following

function preventHandlers(buttonID) {
    $('#'+buttonID).off('click',bar[buttonID]);
}

function loadHandlers(buttonID) {
   $('#'+buttonID).on('click',bar[buttonID]);
}

And you can store them like

bar[buttonID] = function(event){

};

UPDATE:

Though the answers here at How to debug JavaScript/jQuery event bindings with Firebug (or similar tool) and here How to find event listeners on a DOM node when debugging or from the JavaScript code? and others are saying that .date('events') returns all event handlers, i could not get it to work.

I have set up an example with the way stated above. Chek the following link

Working Fiddle

like image 152
Prasenjit Kumar Nag Avatar answered Jan 25 '26 22:01

Prasenjit Kumar Nag



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!