Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
jQuery keypress binding works only when a breakpoint is set in Visual Studio debugger

I think there must be a race condition but don't know how to fix it.

I've got several INPUTs lined up in row, each having a maxlength =1, and I want to move through them as the user types an alphabetic letter:

      /* jQuery 3.6.0 */
      $("input").bind("keypress", function (e) {
            if (/[A-Z]/i.test(e.key)) {
              /* breakpoint on this next line */
                $(this).next('input').focus();  
            }
        });

If I put a breakpoint on the line as indicated, the debugger stops there as expected, and if I press [F5] to continue, focus is indeed advanced to the next INPUT. But if I remove the breakpoint, and run the program normally, the focus does not advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Problem you have is you have a race condition of the action of the event and the focus. You can get around it with a timeout

$("input").bind("keypress", function(e) {
  if (/[A-Z]/i.test(e.key)) {
    window.setTimeout(() => $(this).next('input').focus(), 0);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda