Monday, May 30, 2005

Cursor In The Form Field?

Ever wondered how they get the cursor to automatically flash in the first input field of a form?

In order for a user to fill out a form field, he/she must first give it focus. This is achieved by clicking in the form field. Only then can they enter their data. This snippet will automatically give focus to the first form field.

Take the contents below and add it to the <head> area of your webpage. Then replace your <body> tag with the complete <body onload="setfocus()"> tag below. That's it. The form that is shown below is an example that you can use to test the effectiveness of this little script.

<script type="text/javascript">
function setfocus()
{
document.forms[0].field.focus()
}
</script>

<body onload="setfocus()">
<form>
<input type="text" name="field" size="30">
<input type="text" name="field1" size="30">
<input type="text" name="field2" size="30">
<input type="text" name="field3" size="30">
<input type="text" name="field4" size="30">
</form>

Using techniques like this one make your site easier to use, one of a webmaster's main goals. When you are asking a person to fill out a form you should save them a mouse click by predicting their needs and providing 'focus' to the first field in your form automatically.

0 Comments:

Post a Comment

<< Home