Click Once and Unbind

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Have something happen on a click event, but only once! Unbind the click handler after the element has been clicked once.

$('#my-selector').bind('click', function() {
       $(this).unbind('click');
       alert('Clicked and unbound!');
});

Also see Ben’s comment below for jQuery’s .one() function which is essentially the same thing.