I have a question in react/JavaScript that when I press Tab on the keyboard, I need the Enter key to be triggered or active, so any idea how can I do it.
Asked
Active
Viewed 3,059 times
0
-
https://stackoverflow.com/questions/31272207/to-call-onchange-event-after-pressing-enter-key – tony_merguez Jan 17 '22 at 11:23
-
@Sam RQ is my answer working for you? – gru Jan 21 '22 at 12:44
1 Answers
2
Although I'm not sure what you're motivation for this behavior is, your logic is even possible with Vanilla JS:
- Simplest way to detect keypresses in javascript
- Is it possible to simulate key press events programmatically?
Minimal example for your use case:
- Click into the input field
- Now press
tab
- You will see an alert that tab was pressed
- Immediately after, you will see that
enter
was pressed as well (programmatically)
let element = document.querySelector('input');
element.onkeydown = e => {
alert('key pressed: ' + e.key);
if (e.key === 'Tab') {
element.dispatchEvent(
new KeyboardEvent('keydown', {
'key': 'enter'
}));
}
}
<input/>

gru
- 2,319
- 6
- 24
- 39
-
1While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/30837408) – Jamiec Jan 17 '22 at 17:00
-