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>