Skip to content

Instantly share code, notes, and snippets.

@joshperry
Created February 25, 2014 17:19
Show Gist options
  • Save joshperry/9213470 to your computer and use it in GitHub Desktop.
Save joshperry/9213470 to your computer and use it in GitHub Desktop.
angular.module('pslm')
.directive('pdkNextInputOnEnter', function() {
var includeTags = ['INPUT', 'SELECT'];
function link(scope, element, attrs) {
element.on('keydown', function (e) {
// Go to next form element on enter and only for included tags
if (e.keyCode == 13 && includeTags.indexOf(e.target.tagName) != -1) {
// Find all form elements that can receive focus
var focusable = element[0].querySelectorAll('input,select,button,textarea');
// Get the index of the currently focused element
var currentIndex = Array.prototype.indexOf.call(focusable, e.target);
// Find the next items in the list
var nextIndex = currentIndex == focusable.length - 1 ? 0 : currentIndex + 1;
// Focus the next element
if(nextIndex >= 0 && nextIndex < focusable.length)
focusable[nextIndex].focus();
return false;
}
});
}
return {
restrict: 'A',
link: link
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment