SWE 432 JavaScript example: Focus Example

Text version Next example Back to index


HTML does not have a command to set the focus in a field, but JavaScript does.

First name:         Last name:

<HEAD>
<TITLE>Focus Example</TITLE>
   <SCRIPT type="text/javascript">
     function setFocus()
     {
       document.focus.firstName.focus();
     }
   </SCRIPT>
</HEAD>

<BODY onload="setFocus()">

<P>
<FORM method="post" name ="focus" action="">
   First name: <INPUT type="text" name="firstName">
   &nbsp; &nbsp; &nbsp; &nbsp;
   Last name: <INPUT type="text" name="lastName">
</FORM>

</BODY>
</HTML>