Skip to content

Instantly share code, notes, and snippets.

@ChongTang
Created July 21, 2015 01:47
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 ChongTang/55fb59a02c1ff2e5b9aa to your computer and use it in GitHub Desktop.
Save ChongTang/55fb59a02c1ff2e5b9aa to your computer and use it in GitHub Desktop.
AngularJS auto focus on another input when an input's text reach certain length
// The AngularJS code
var app = angular.module('autofocus', []);
app.directive('autofocusWhen', function () {
return function (scope, element, attrs) {
scope.$watch('maxLengthReach', function(newValue){
if (newValue.length >= 5 ) {
element[0].focus();
}
});
}
});
// The HTML code
<div ng-app="autofocus">
<label>Name:</label>
<input ng-model="maxLengthReach"></input>
<br/><br/>
<label>Title:</label>
<input autofocus-when></input>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment