showing results for - "change the focus to next in angular forms"
Luigi
05 Jan 2019
1import { Directive, ElementRef, HostListener, Input } from '@angular/core';
2
3@Directive({
4selector: '[moveNextByMaxLength]'
5})
6export class MoveNextByMaxLengthDirective {
7
8constructor(private _el: ElementRef) { }
9
10@HostListener('keyup', ['$event']) onKeyDown(e: any) {
11    if (e.srcElement.maxLength === e.srcElement.value.length) {
12
13        e.preventDefault();
14
15        let nextControl: any = e.srcElement.nextElementSibling;
16       // Searching for next similar control to set it focus
17        while (true)
18        {
19            if (nextControl)
20            {
21                if (nextControl.type === e.srcElement.type)
22                {
23                    nextControl.focus();
24                    return;
25                }
26                else
27                {
28                    nextControl = nextControl.nextElementSibling;
29                }
30            }
31            else
32            {
33                return;
34            }
35        }
36    }
37}
38
39}