set focus to ....

?

=?iso-8859-1?Q?Kim_Andr=E9_Aker=F8?=

Aaron said:
I need to set focus to a input text field on a simple form at onLoad.

This might work:

<body onload="document.getElementByName('textfieldname').focus()">

Then, later in the HTML document:

<form ...>
<input type="text" name="textfieldname">
</form>

Please note that this will only work in browsers that have JavaScript
enabled.
 
S

Steve Pugh

Do you really need to? What would happen if you couldn't?

If you have a very small page, on a fast server, with just one single
form input (i.e. if you're the Google home page) then you can get
away with this. Otherwise you have to consider that on a slow loading
page the user may start typing in a form field and then when the page
finishes loading, your script would place the focus back at the start
of your chosen field. This would lead to the user's typing being
screwed up.

If you are the Google home page then look at your own source code. ;-)
The form is not loaded yet.

Both the HTML spec
<http://www.w3.org/TR/html401/interact/scripts.html#adef-onload> and
observed behaviour in several browsers disagrees with you.

"The onload event occurs when the user agent FINISHES loading a
window" (my emphasis)

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top