Solved

.focus() from onSubmit

Posted on 1998-04-22
8
373 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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 …
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…

760 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