2

I need to be able to remove a function on a click event.

I have looked at the unbind and nullifying but I am unsure of the best solution, is there a destroy ability - I can't seem to find one. I need to be able to run the function again when I click .button

$('.button').on('click', function (e) {
    run();

    $('.back').on('click', '.back', function () {
        //remove the run function
    });
});

function run() {
    //code
}

I have also tried nullifying the function but it doesn't remove the event fully,

run = null;

run = function () {};

Thank you

5 Answers 5

1

You can use jQuery.unbind('click') to remove a previously-attached event handler from the elements.

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

1 Comment

what happens if he has other logic in that event beside the run function?
1

You can use jQuery.noop():

run = $.noop();

Comments

0

Try:

var isRun = 0;
$('.button').on('click', function (e) {
   if(isRun == 0) {
    run();
} 
});
  $('body').on('click', '.back', function () {
       isRun = 1;
    });
function run() {
    //code
}

Comments

0
$('.back').on('click', '.back', function () {
        function run() {}//empty function will override original function on click
    });

Comments

0

The previous answer may work. If not, try this: run().remove

Comments

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.