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

167
Visualizações
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
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