Created
March 9, 2017 12:18
-
-
Save duncanmcdougall/695336c7e53d813762f5b6f90d84f754 to your computer and use it in GitHub Desktop.
Angular 1 Autotab On Maxlength Directive
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
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