| $ | Getting focus in HTML forms | Last modified: Fri Jan 28 18:37:45 2000 | |||||||||
Getting focus in HTML formsTypically, when a form gets loaded by a browser, none of the form's elements get the focus by default. This can be annoying for users because they have to click on one of the input elements to begin entering data into the form. And users are lazy, so we'd like to make our pages require as few body movements as possible. Two Methods for Form Focus'ing and Navigation
There are two main ways to make form navigation easy: using JavaScript to
give input elements the focus and using the JavaScript
|
|||||||||||
| Rendered | Code |
|---|---|
<form action="/cgi-bin/beaver.cgi" name="form1">
<input type="TEXT" name="Element1" size="10" value="First Tab" tabindex="0">
<input type="TEXT" name="Element2" size="10" value="Second Tab" tabindex="1">
<input type="submit" name="submit_beaver" value="Submit" tabindex="2">
</form>
<script language="javascript">
<!--
document.form1.Element1.focus()
//-->
</script>
|
(As usual, Netscape doesn't seem to be taking care of elements in
tables correctly. When I open this form in Communicator 4.04/Linux,
the call to focus does not work. However, if I take the form out of
the table, Element1 does load focused.)
Here's a form that jumps around (non top-down),
| Rendered | Code |
|---|---|
<form action="/cgi-bin/beaver.cgi" name="form2">
<input type="TEXT" name="Element1" size="10" value="First Tab" tabindex="0">
<input type="TEXT" name="Element2" size="10" value="Sixth Tab" tabindex="5">
<input type="TEXT" name="Element1" size="10" value="Fifth Tab" tabindex="4">
<input type="TEXT" name="Element2" size="10" value="Fourth Tab" tabindex="3">
<input type="TEXT" name="Element1" size="10" value="Third Tab" tabindex="2">
<input type="TEXT" name="Element2" size="10" value="Second Tab" tabindex="1">
<input type="submit" name="submit_beaver" value="Submit" tabindex="7">
</form>
|
tabindex
B.6.1: Notes on Forms
focus()