Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery last focused element

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.

like image 880
Elliott Avatar asked Jan 16 '11 19:01

Elliott


1 Answers

Subscribe all of your inputs to the focusout event

$('.input').focusout(function () {
   $('#myform').data('lastSelected', $(this));
});
like image 158
Vadim Avatar answered Sep 19 '22 00:09

Vadim