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.
Subscribe all of your inputs to the focusout event
$('.input').focusout(function () {
$('#myform').data('lastSelected', $(this));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With