Last focused jquery element

I have a basic login form that uses ajax to login. If the details are incorrect, a text is displayed on which the login error has failed. When this is shown by the last text box, the user enters the text, losing focus.

Is it possible to set focus on the last text field that was included by the user before clicking the "Send / Press" button?

$('#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.

+6
jquery setfocus
source share
1 answer

Sign all your inputs to the focus event

 $('.input').focusout(function () { $('#myform').data('lastSelected', $(this)); }); 
+7
source share

All Articles