Looking for a smoother way to focus the next input element in Angular without manually specifying which one. Here's my current HTML setup...
<div class="mb-2 digit-insert d-flex align-items-center">
<div class="confirmation-group d-flex">
<div class="digit-wrapper">
<input #digitOne type="text" (paste)="onDigitPaste($event)" maxlength="1"
(keyup)="onDigitInput($event, null, digitTwo)" />
</div>
<div class="digit-wrapper">
<input #digitTwo type="text" maxlength="1" (keyup)="onDigitInput($event, digitOne, digitThree)" />
</div>
<div class="digit-wrapper">
<input #digitThree type="text" maxlength="1" (keyup)="onDigitInput($event, digitTwo, digitFour)" />
</div>
</div>
<span class="confirmation-divider m-3">-</span>
<div class="confirmation-group d-flex">
<div class="digit-wrapper">
<input #digitFour type="text" maxlength="1" (keyup)="onDigitInput($event, digitThree, digitFive)" />
</div>
<div class="digit-wrapper">
<input #digitFive type="text" maxlength="1" (keyup)="onDigitInput($event, digitFour, digitSix)" />
</div>
<div class="digit-wrapper">
<input #digitSix type="text" maxlength="1" (keyup)="onDigitInput($event, digitFive, null)" />
</div>
</div>
</div>
The key up event determines which input field to focus on after user input. Here's the relevant TypeScript code...
onDigitInput(event: any, previousElement: any, nextElement: any): void {
if (event.code !== 'Backspace' && nextElement !== null) {
nextElement.focus();
}
if (event.code === 'Backspace' && previousElement !== null) {
previousElement.focus();
previousElement.value = '';
}
}
Curious if there's a better approach or directive to achieve this focus functionality?