?
Solved

Set Focus on a Form

Posted on 2003-03-20
8
Medium Priority
?
165 Views
Last Modified: 2013-12-24
When my form is accessed I would like the cursor to be in the first input box on the form.  Does anyone know how to do this?  

Thanks.
Cindy
0
Comment
Question by:swartout
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Expert Comment

by:danrosenthal
ID: 8176142
you can do it like this...

document.myform.elements[0].focus();

0
 
LVL 15

Expert Comment

by:danrosenthal
ID: 8176154
in the above example "myform" is the name of your form.

You can also set the focus to particular input by name like this...

document.myform.fieldname.focus();

0
 

Author Comment

by:swartout
ID: 8176317
what tag would I use with this?  Where would I put it on my form?
0
Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

 
LVL 10

Expert Comment

by:substand
ID: 8176576
you can either put it in the <body onload="document.myform.elements[0].focus();">

or you can just put a

<script>
document.myform.elements[0].focus();
</script>
0
 
LVL 10

Expert Comment

by:substand
ID: 8176578
the script is supposed to go in the <head> section
0
 
LVL 15

Accepted Solution

by:
danrosenthal earned 200 total points
ID: 8176934
One important thing to note is the order in which things happen, generally a page is read by the browser from top to bottom. So if you put a command at the top of the page concerning form elements that have not been written yet it will trigger an error.  So either put this at the bottom of the page...

<SCRIPT language="JavaScript">
   document.myform.elements[0].focus();
</script>

Or as suggested by substand, put it in as an ONLOAD event in the BODY tag (which will get called only after the page is completely loaded...

<body onload="document.myform.elements[0].focus();">

However, I think it is generally a better practice to call a funtion rather than a direct javascript statement like so...

<body onload="startup();">

<SCRIPT language="JavaScript">
function startup(){
     document.myform.elements[0].focus();
}
</script>

(in this case the javascript function can be placed anywhere on the page as it will only get called after the page is fully loaded)



0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 8178251
They all have the same idea with this mine...
Take this example...

<body onLoad="document.form1.user_name.focus()">
<form name='form1'>
  <input name="user_name" type="text">
  <input name="user_pass" type="password">
</form>
</body>

But if you also want that when you press enter key, it will focus to the next field. I have a javascript code for this...

I hope this example will help...
GOODLUCK!
0
 

Author Comment

by:swartout
ID: 8178935
I placed the code at the bottom of the page and it works like a charm.  Thanks for your help.
0

Featured Post

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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