Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

81
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos