how to unbind and bind again

16,293

Solution 1

You have to keep a reference to the function (instead of passing an anonymous function):

function handler() {
    // do something
}

$("#archive").click(handler); // bind the first time
$("#archive").unbind('click', handler); // unbind
$("#archive").click(handler); // bind again

Not sure what is event in your case, but if it is the event object passed to the event handler then it does not make sense to pass it to unbind and bind.

Solution 2

As of jQuery 1.7 you should use on and off for all your event handler binding:

var handler = function() {
  alert('The quick brown fox jumps over the lazy dog.');
};
$('#foo').on('click', handler);
$('#foo').off('click', handler);
Share:
16,293
tep
Author by

tep

Updated on June 04, 2022

Comments

  • tep
    tep almost 2 years
    $("#archive").click(function(event){
            /*do something*/
    });
    
    $('#archive2').unbind('click',event);
    

    i have this click function that I unbind. however, i want to bind it again when i click a certain button.

    $("#archive").bind("click",event);
    

    im using this code to bind again, however it doesn't seem to work. any suggestions or workarounds?

  • KTF
    KTF almost 13 years
    @tep: You're welcome! Looks like you're new here- welcome to the community; remember to mark an answer and vote up any useful posts.