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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great suggestion! Worked perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.