Created
October 8, 2014 23:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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