?
Solved

.focus() from onSubmit

Posted on 1998-04-22
8
Medium Priority
?
385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 5

Expert Comment

by:julio011597
ID: 1266905
...or, if i missed the point!
0
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

770 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