Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

301
Vistas
Dispatch event on an input element while pressing tab key is not working in JavaScript

I am trying to dispatch tab key Press event pro grammatically on a button click. When the button is clicked, I provided focus for the first input and dispatched key press event with tab key and try to focus the second input. But unfortunately this is not working.

document.getElementById('btn').addEventListener('click', function() {
  const input = document.getElementsByTagName('input')[0];
  input.focus();
  alert('Input 1 focused');
  input.dispatchEvent(
    new KeyboardEvent('keypress', {
      key: 'tab',
      code: 'tab',
      keyCode: 9
    })
  );

});
<button id="btn">click</button>
<input id="default" tabindex="0" />
<br />
<br />
<br />
<input id="default1" tabindex="0" />

I expect upon button click, the second input should be in focus state since I have dispatched tab keypress there.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The reason for this is that your event is not Trusted. isTrusted property of the event is false. This is a readonly property.

More details you can find here JavaScript trigger an InputEvent.isTrusted = true

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda