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.