Link to home
Start Free TrialLog in
Avatar of HUMMELS
HUMMELS

asked on

.focus() from onSubmit

I'm trying to set the focus to a particular Form Input object in a form's Validation Routine.
              TheForm.elements[i].focus();
              TheForm.elements[i].select();

My validation routine is called from the onSubmit() of the <FORM> tag.  It appears to very briefly give the focus to the correct intput object, but then appears to have the focus changed to the submit button.

Any clues?
Avatar of julio011597
julio011597

That works fine for me... you should show your validation routine.
Avatar of HUMMELS

ASKER

Here is the complete HTML.  I've cut it down to the basics

<HTML>
<HEAD>

<SCRIPT LANGUAGE="JavaScript">
function Validate(TheForm) {
   for (var i=0; i < TheForm.elements.length-1; i++) {
      if (TheForm.elements[i].value.length==0){
         alert("you must fill something in!");
         TheForm.elements[i].focus();
         TheForm.elements[i].select();
         return false;
      }
   }
   alert("OK!");
   return(true);
}
</SCRIPT>

</HEAD>
<BODY>

<FORM NAME="displayorder" onSubmit="return Validate(this)">
<INPUT TYPE="text" NAME="one" VALUE="ONE">
<INPUT TYPE="text" NAME="two" VALUE="TWO">
<INPUT TYPE="text" NAME="three" VALUE="THREE">
<INPUT TYPE="text" NAME="four" VALUE="FOUR">
<INPUT TYPE="submit">
</FORM>

</BODY>
</HTML>

ASKER CERTIFIED SOLUTION
Avatar of julio011597
julio011597

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
...or, if i missed the point!
Avatar of HUMMELS

ASKER

In my example, I've got 4 text inputs.  The submit also gets considered as an input object.  That is why I'm doing the TheForm.elements.length-1.  So it will go through all the inputs except the submit button, which is the last one.
Ok, right, my fault. And, feel free to reject my answer.

This said, again, the code is valid.

Maybe you could tell which OS/Browser combination you are trying it on?
Did you try it on a different browser?
Is the page accessible on-line, for a test?
Do you have the complete code with the forms to? : )
Ignore the comment above. I was not awake. Sorry!