I'm trying to setup an input, such that when length == 5, it will automatically trigger a blur event. How can I do this?
This is pretty much the concept:
<input type="tel" placeholder="type here." maxlength="5" name="digits" ng-model="digits" ng-change="if (digits.length ==5) { TRIGGER BLUR};">
Thanks
The blur event fires when an element has lost focus. The main difference between this event and focusout is that focusout bubbles while blur does not. The opposite of blur is focus . This event is not cancelable and does not bubble.
A blur event fires when an element has lost focus. Note: As the blur event is executed synchronously also during DOM manipulations (e.g. removing a focussed input), AngularJS executes the expression using scope.
blur( [eventData ], handler ) An object containing data that will be passed to the event handler. A function to execute each time the event is triggered.
The main difference between this event and blur is that focusout bubbles while blur does not. The opposite of focusout is focusin .
Here is a basic directive that should get you what you are looking for:
app.directive('lengthChecker', function() {
return function(scope, element, attributes) {
scope.$watch(attributes.lengthChecker, function(newVal, oldVal) {
if (newVal.length >= 5) element[0].blur();
});
};
});
Html:
<input ng-model="digits" length-checker="digits"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With