jQuery unbind then bind

ialphan picture ialphan · Oct 15, 2012 · Viewed 9.6k times · Source

I'm unbinding click from these clickable divs and then want to enable clicking again. What I have doesn't work. Any suggestions? Thanks.

    $('#a, #b, #c').on('click', function(e){
        $('#a, #b, #c').unbind('click');
        // some stuff
        // bind again: 
        // this doesn't work: $('#a, #b, #c').bind('click');
    }

Answer

Selvakumar Arumugam picture Selvakumar Arumugam · Oct 15, 2012

You are missing the handler function // this doesn't work: $('#a, #b, #c').bind('click'); when you try to rebind.. what you need is something like below,

$('#a, #b, #c').bind('click', clickHandlerA);

function clickHandlerA() {
   $('#a, #b, #c').unbind('click');
   // some stuff
   // bind again: 
   // this should work: 
   $('#a, #b, #c').bind('click', clickHandlerA);
}