I am trying to create a function that auto focuses an input field when the page is loaded and reloaded.
I have existing code that when an input box is filled out and it loses focus, the page reloads. It will then put focus back into the first input box and this is an issue I want to correct.
In the main body of the Jquery load function I call focusForm();
function focusForm() {
$('#txtFName #txtLName #txtDistID #txtPostalCode #txtEmail').each(function () {
var currVal = $(this).val();
if (currVal == '') {
$(this).blur();
return false;
}
});
}
What I am trying to accomplish is, when the page is loaded/reloaded it will check each input, when it finds one that has no value it will focus that box.
Any idea what I am missing?
You want
$('#txtFName, #txtLName, #txtDistID, #txtPostalCode, #txtEmail')to select all of the different elements. The selector you used looks for an element with idtxtEmailinside one with idtxtPostalCodeinsidetxtDistIDinsidetxtLNameinsidetxtFName.Also, I think you want
focusinstead ofblur.