Using javascript to automatically put the cursor in the first field of a form
When creating a form for use on a webpage, it may be desirable to have the cursor
automatically appear in a certain field. To do this requires a bit of javascript.
This example is from a CGI script that creates the form.
#!/usr/bin/perl
use CGI qw(:standard);
print header();
print start_html("CookieMonster");
print <
Please enter your login ID. This is the same as your network login. A password
is not necessary.
EndOfHTML
print end_html;
Here are the key points:
The form has to have a name--see the 'name="form"' part.
In the body of the form, use the 'onload' directive. The syntax for the
value is the word 'document' followed by the name you gave the form, followed by the
name you gave the desired field, followed by 'focus(), all connected with dots.
Somewhere in the form, declare javascript as the script type used.