Self removing anonymous event handler

Posted on 23 September 2012 in Problem and solution

I was doing some jQuery and wanted a event handler that removed itself after the first time the event was triggered. Turns out you can do this with the event object and the unbind function.

// Add a anonymous event handler
$('#mybutton').on('click', function(event) {
	// Remove this function by calling unbind
	$(this).unbind(event);
});