Automatically Put the Cursor in a Form Field

Posted on October 2, 2006

{{LEARN JAVASCRIPT HERE}}

Setting the focus, or for beginners, automatically placing the cursor in one of the fields of your HTML form, is fairly simple. There are two common methods. The first and most common is to use the <BODY> tag’s onload event.

Ex.
<body onload="document.FORMNAME.FIELDNAME.focus();">

However, there are times that you cannot use the body tag and you need another solution. As an alternative to the onLoad event, you can use the setTimeout fuction in JavaScript to wait half a second and then set the focus.

Ex.
<script>
setTimeout(’document.FORMNAME.FIELDNAME.focus()’,500);
</script>

In both cases, you would replace FORMNAME with the value of the name attribute in the <form> tag. You replace FIELDNAME with the value of the name attribute in the <input> tag of the targeted field.

 

Filed Under JavaScript |

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments