Skip to content

Instantly share code, notes, and snippets.

@andrewburgess
Created May 21, 2013 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewburgess/5621785 to your computer and use it in GitHub Desktop.
Save andrewburgess/5621785 to your computer and use it in GitHub Desktop.
KnockoutJS binding that enables handling enter key presses and performing an action
ko.bindingHandlers.executeOnEnter = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var allBindings = allBindingsAccessor();
$(element).keypress(function (event) {
var keyCode = (event.which ? event.which : event.keyCode);
if (keyCode === 13) {
ko.utils.triggerEvent(element, 'change');
allBindings.executeOnEnter.call(viewModel, viewModel, event);
return false;
}
return true;
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment