In jQuery, is there any way to only bind a click once?

User jQuery one function like Tom said, but unbind the handler each time before binding again. It helps to have the event handler assigned to a variable than using an anonymous function.

 var handler = function(e) { // stuff };

 $('#element').unbind('click', handler).one('click', handler);

 //elsewhere
 $('#element').unbind('click', handler).one('click', handler);

You can also do .unbind('click') to remove all click handlers attached to an element.

Leave a Comment