Question

I have a basic login form, which uses ajax to login users. If the details are incorrect text is displayed showing login has failed. When this is shown the last textbox the user entered text in loses focus.

Is it possible to set focus to the last textbox which the user was on before pressing submit/pressing enter?

$('#login').click(function (e) 
 { 
  e.preventDefault();
  $.ajax({
  type: "POST",
  dataType: "text",
  url: "login.php",
  data: "username=" + $("#username").val() + "&password=" + $("#password").val(),
  cache: false,
  success: function(reply)
  {
   if (reply.indexOf("Success") >= 0)
   {
    location.reload();
   }
   else
   {
    $("#loginResult").html("Login Failed");
    //set focus here       
   }
  }
 }); 

Any suggestions, Thanks.

Was it helpful?

Solution

Subscribe all of your inputs to the focusout event

$('.input').focusout(function () {
   $('#myform').data('lastSelected', $(this));
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top