Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.focus() from onSubmit

Posted on 1998-04-22
8
Medium Priority
?
389 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
Technology Partners: 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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

610 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