I have a scenario where I have two or more textarea, when user enters the value in the textarea and when the values reaches to max-length for example ng-max-length is 15, the focus should automatically move to next text-area.
How this can be achieved?
For controlling the max-length I have taken solution from this link .
But i do not know how to make the focus to next element automatically
Below i have given the code which i tried
textarea.html
<!DOCTYPE html>
<html>
<head>
<title>Angular App</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<div ng-app="taApp">
<div ng-controller="taController">
<textarea my-maxlength="15" ng-model="ta1.text1" rows="4" cols="20"> </textarea>
<textarea my-maxlength="15" ng-model="ta2.text2" rows="4" cols="20"> </textarea>
<textarea my-maxlength="15" ng-model="ta3.text3" rows="4" cols="20"> </textarea>
</div>
</div>
<script src="js/libs/angularjs-1.0.2/angular.js" type="text/javascript"></script>
<script src="js/controller/pageController.js" type="text/javascript"></script>
<script src="js/controller/textAreaFocus.js" type="text/javascript"></script>
</body>
</html>
textAreaFocus.js
angular.module('textareafocus',[]).directive('myMaxlength', function() {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
var maxlength = Number(attrs.myMaxlength);
function fromUser(text) {
if (text.length > maxlength) {
var transformedInput = text.substring(0, maxlength);
ngModelCtrl.$setViewValue(transformedInput);
ngModelCtrl.$render();
return transformedInput;
}
return text;
}
ngModelCtrl.$parsers.push(fromUser);
}
};
});
pageController.js
var taApp = angular.module('taApp',["textareafocus"]); // this creates a new angular module named "myApp";
taApp.controller('taController', function ($scope,$http) {
$scope.ta1 = { text1: 'TextArea 1'};
$scope.ta2 = { text2: 'TextArea 2'};
$scope.ta3 = { text3: 'TextArea 3'};
});
Screenshot
Currently i have only 3 textarea but it could be more, therefore focus should move to next element automatically.
I tried with element[ 1 ].focus() but it doesn't works.
Kindly help me how to resolve this issue.