Wednesday, 1 March 2017

Focus On Next Input On Enter

No comments
Directive


//move to next input when enter
app.directive('focus'function () {
    return {
        restrict: 'A',
        link: function ($scope, elem, attrs) {
            elem.bind('keydown'function (e) {
                var code = e.keyCode || e.which;
                if (code === 13) {
                    e.preventDefault();
                    //          elem.next().focus();
 
                    if (elem.attr('id') == "registerEmail") {
                        $(':input:eq(' + ($(':input').index(this) + 1) + ')').on('focus'function () {
                            $(this).attr('type''date');
                        });
                        $(':input:eq(' + ($(':input').index(this) + 1) + ')').focus().click();
                    }
                    else {
                        $(':input:eq(' + ($(':input').index(this) + 1) + ')').focus();
                    }
                }
            });
        }
    }
});

HTML

<label>
  <input id="email" focus type="email" ng-model="userEmail" required>
/label>
<label>
  <input id="password" focus type="password" ng-model="userPassword" required>
</label>