Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set focus to form field

Status
Not open for further replies.

TMac42

Programmer
Jul 24, 2000
83
US
There are a gazillion examples of how to set Focus to a form field in the <BODY ONLOAD..... but my page currently does other JavaScript in the OnLoad. But, when the page opens up, I need to set focus to a form field. What are my options??

Thanks all!
 
not really the most recommended but you can place this after the closing form tag
<script language=&quot;javascript&quot;>
document.form name.field name.focus();
</script> _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
you can also put all of the code you have in your onload event into a function, then add

document.form_name.field_name.focus();

to the end of that function, then have the onLoad event call that function.

Personally, I like to make a startup function whenever I'm going to use the onLoad event of a page so that I can easily muck with that code.

-Rob
 
see the simple things pass before our eyes with a blank stare in space as we need to make things harder then they may appear. [lol][lol]

good call sim777 _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
Okay, here's my issue... I'm putting his JavaScript into a head.asp page, and <BODY onload> is in body.asp and they're both #include files for my main ASP page. First, thanks for the responses thus far. I tried the script after the </form> tag and I also tried Body onload w/ a function. Neither one seems to work and I'm boggled. Is it because the function cannot find the form since it's in a dif page? Still not sure why onpnt's original idea won't work either because that's in the same page as the form. Hmmm. Thoughts????
 
Actually, ASPs files are processed on the server site. Browser gets the united code as one file. You can see it with View Source option of your browser. So, the problem is not there, the problem is on the client site.
Browser builds the page objects (DOM) dynamically while browser reads the HTTP stream. So, if you put the code such
<script language=&quot;javascript&quot;>
document.form.field.focus();
</script>

in the HEAD section, it should not work, because the form objects are not built yet. However, you can put this code just after the form element you want to set focus on.
See the following code:

<HTML>
<HEAD></HEAD>
<BODY>
<form name=menufrm>
<input type=&quot;text&quot; name=a value=&quot;&quot;><br>
<input type=&quot;text&quot; name=b value=&quot;&quot;><br>

<script>document.forms.menufrm.b.focus();</script>
<input type=&quot;text&quot; name=c value=&quot;&quot;><br>
<input type=&quot;submit&quot; value=&quot;Submit It&quot;>
</BODY>
</HTML>


Regards,
Sergey Smirnov
Visit my forum at:

P.S. One possible way is to overwrite the onload function, but it is another store.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top