Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!