I have an form with several text-input fields. I'd like that when the user presses the ENTER key, you jump to the next input field (keeping the text introduced, same behaviour as with the tab key) and when you press the ESC key you clear the input field.
The ESC key is the same as this question, but for Angular2, not AngularJS.
It is also similar to this one, but with the keyboard, not with a button.
My idea to do it was the following:
<input type="text" formControlName="whatever" id="whatever"
class="form-control" autocomplete="off" (keyup)="onKey($event)" required>
and then in the .ts
:
public onKey(event: { key: string; }) {
if (event.key === 'Enter') {
I DON'T KNOW WHAT TO DO (1);
}
if (event.key === 'Escape' || event.key === 'Esc') {
I DON'T KNOW WHAT TO DO (2);
}
}
where I DON'T KNOW WHAT TO DO (1)
should "focus on the following input/button" and I DON'T KNOW WHAT TO DO (2)
"should clear the cell and focus out".
The event.key === 'Escape' || event.key === 'Esc'
is to consider both Chrome and IE. I still have to check other options for different browsers, but if you have suggestions, they'll be appreciated too! ;-).
If I should split my question into two different questions (one for the ENTER, one for the ESC), let me know it and I'll do it. Thanks!
UPDATE: I found this question, but the answer relies on the nextElementSibling
which does not work in my case (the input fields are not siblings). I also found this similar question but some of the elements involved are deprecated already.