Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular js trigger blur event

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

like image 842
Federico Avatar asked Apr 25 '14 18:04

Federico


People also ask

What triggers blur event?

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.

What is blur event in angular?

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.

Is blur an event handler?

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.

What is the difference between Blur and Focusout?

The main difference between this event and blur is that focusout bubbles while blur does not. The opposite of focusout is focusin .


1 Answers

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"/>
like image 54
Brian Lewis Avatar answered Sep 19 '22 06:09

Brian Lewis