Skip to content

Instantly share code, notes, and snippets.

@duncanmcdougall
Created March 9, 2017 12:18
Show Gist options
  • Save duncanmcdougall/695336c7e53d813762f5b6f90d84f754 to your computer and use it in GitHub Desktop.
Save duncanmcdougall/695336c7e53d813762f5b6f90d84f754 to your computer and use it in GitHub Desktop.
Angular 1 Autotab On Maxlength Directive
myApp.directive("autotabOnMaxlength", function () {
return {
restrict: "A",
require: "ngModel",
link: function ($scope, element, attr, ctrl) {
ctrl.$viewChangeListeners.push(function () {
if (element.val().length == element.attr("maxlength")) {
var elems = Array.prototype.filter.call(document.querySelectorAll('input, select, textarea, button, [tabindex]'), function(elem) {
return (elem.offsetParent != null) && !elem.disabled;
});
var idx = elems.findIndex(function(item) {
return item == element[0];
})+1;
if(idx < elems.length && idx > 0) {
elems[idx].focus();
}
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment