Created
February 25, 2014 17:19
-
-
Save joshperry/9213470 to your computer and use it in GitHub Desktop.
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
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