advertisement -- please support sponsors

forms, performance:
automatically giving "keyboard focus" to the first element in a form

How can I make the first element of a form automatically receive keyboard focus when the form is loaded?

Most form elements (i.e., text boxes, radio buttons, etc.) have a method called focus which can be used just for this purpose. Simply add an onLoad event to your document's <body> tag so that the focus method is called when the form is loaded.


The following source describes a "login form" that automatically places keyboard focus in the "username" field:
<body onLoad="document . loginform . username . focus ();">

Please identify yourself:

<form name=loginform>
username: <input name=username type=text>
password: <input name=password type=password>
          <input type=submit value="OK">

When the form is completely loaded, the <body> tag's onLoad event will fire, which in turn will give keyboard focus to the username element of the form.

Charlton Rose
April 16, 1997