0

Is there any way to 'disable' or 'suspend' a jquery .click function, without unbinding? I want to leave the function bound, but don't want it to fire. Then, at a later time, I would like to re-enable it.

Just disabling the control being bound to will not suffice in this case.

tmaurst
  • 552
  • 3
  • 14
  • 34

1 Answers1

1

You could set a flag in a higher variable scope and only execute the handler if the flag is on. Here's a simple example:

$(document).ready(function () {
    var execHandler = false;
    $('input').click(function (e) {
        if (execHandler) {
            //do stuff
        }
        e.preventDefault();
        return false;
    });
    $('button#toggleHandler').click(function (e) {
        execHandler = !execHandler;
        e.preventDefault();
        return false;
    })
});

It's a similiar solution to: Suppress jQuery event handling temporarily

Community
  • 1
  • 1
pete
  • 24,141
  • 4
  • 37
  • 51