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 calledfocus
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.
When the form is completely loaded, the
<body onLoad="document . loginform . username . focus ();"> Please identify yourself: <form name=loginform> <pre> username: <input name=username type=text> password: <input name=password type=password> <input type=submit value="OK"> </pre> </form> </body>
<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