Thursday, January 18, 2007

Form Field Focus On Page Load

Sorry for not posting in so long... I've been lazy, or, rather, busy with other stuff and lazy about RetroWebDev.

In any case, here's another (hopefully) useful tip; how to get the curser to be in a form field on a page load. I find it especially useful for admin type pages that require a log in. You can have everything ready to go for the user to just start typing.

<BODY onLoad="document.forms.MyForm.UserName.focus()">
In this example, your form would look like this:
<form method="post" name="MyForm" action="YourActionPage.asp">
<input type="text" name="UserName" size="15">
<input type="text" name="Password" size="15">
<input type="submit" value="Search">
</form>
This is a simple log in form with two fields: UserName and Password. In this example, when the page loads, the cursor will be placed in the UserName field.

Remember! JavaScript is case sensitive!