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

165
Views
focus method on text box not actually sets the focus

I have a strange behaviour here with focus method. I Just want to understand why it is happening like this.

Case 1: With the below code when I come out of last element i.e driveD hyperlink ,focus is not getting set to first text box even though I applied .focus() to first text box.

function lastFocusOut() {
        alert('focus out ,setting focus to first text box');
        document.getElementById('nameId').focus();
    }
input,a,button{
display:block;
margin:10px;
}
<form>
        <input id="nameId"   type="text" name="name"> <a href="c:\">drive</a>
        <input id="nameId1" type="text" name="name">
        <input type="submit" value="test" />
        <input type="text" name="name"> <a href="d:\">driveE</a> </form> <a href="d:\" onblur="lastFocusOut()">driveD</a> </body>

Case 2: When I add a buttton after the last hyperlink It sets the focus back to first text box.

function lastFocusOut() {
        alert('focus out ,setting focus to first text box');
        document.getElementById('nameId').focus();
    }
input,a,button{
display:block;
margin:10px;
}
<form>
        <input id="nameId"    type="text" name="name"> <a href="c:\">drive</a>
        <input id="nameId1" type="text" name="name">
        <input type="submit" value="test" />
        <input type="text" name="name"> <a href="d:\">driveE</a> </form> 
        <a href="d:\" onblur="lastFocusOut()">driveD</a> </body>
        <button type ="submit" >Submit</button>

I want to understand two things here.

1)Why the focus is not getting set to first text box in case 1.

2)What really makes the difference just by adding the button at the end ,which is making the code work as expected.

about 4 years ago · Juan Pablo Isaza
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!