Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

80
Views
Pressing Tab to active Enter key

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.

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

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/>

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs