Solved

.focus() from onSubmit

Posted on 1998-04-22
8
377 Views
Last Modified: 2006-11-17
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?
0
Comment
Question by:HUMMELS
  • 4
  • 2
  • 2
8 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1266902
That works fine for me... you should show your validation routine.
0
 

Author Comment

by:HUMMELS
ID: 1266903
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>

0
 
LVL 5

Accepted Solution

by:
julio011597 earned 50 total points
ID: 1266904
Well, there's a little misunderstanding in your code:

should be:
for (var i=0; i <= TheForm.elements.length-1; i++)

or better:
for (var i=0; i < TheForm.elements.length; i++)

instead of:
for (var i=0; i < TheForm.elements.length-1; i++)

So, i guess you missed focus on the last field.

Let me know if this is not clear.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1266905
...or, if i missed the point!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:HUMMELS
ID: 1266906
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.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1266907
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?
0
 
LVL 1

Expert Comment

by:andla
ID: 1266908
Do you have the complete code with the forms to? : )
0
 
LVL 1

Expert Comment

by:andla
ID: 1266909
Ignore the comment above. I was not awake. Sorry!

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now