Skip to content

Instantly share code, notes, and snippets.

@mmarinero
Created October 8, 2014 23:22
Show Gist options
  • Save mmarinero/c2b3b46294f4f59db5a8 to your computer and use it in GitHub Desktop.
Save mmarinero/c2b3b46294f4f59db5a8 to your computer and use it in GitHub Desktop.
Jquery plugin to simulate an event that is binded to the enter key
/**
* Jquery enter event plugin.
* It will intercept keypress events but only call the handlers
* if the pressed key is Enter
* Events can be binded with the 'enter' event type or the enter function
* The function accepts an option data parameter tha will be passes to the handler function
* and an event handler. If called without parameters it will trigger the event.
*/
jQuery.event.special.enter = {
bindType: "keypress",
delegateType: "keypress",
handle: function(event, data){
if (event.which == 13 || !event.originalEvent){
return event.handleObj.handler(event, data);
}
}
};
jQuery.fn.enter = function( data, fn ) {
return arguments.length > 0 ? this.on('enter', null, data, fn ) : this.trigger('enter');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment