Skip to content
August 12, 2005 / dshuck

Adding field focus to every form on your site

Someone on an email list I follow posted up question asking for ideas on a good way to make every form in their application display with the focus set to the first field.  He had hit a bump since he was working in fusebox and needed this to work using a common header file.  My suggestion was that he use an onload event that checks to see if there is a form on the page, and if there is a form, focus() the first element.  I thought this might be a nice snippit to hold onto and share.

<script>
function setFocus() {
if ( typeof ( document.forms[0] ) != “undefined” ) {
document.forms[0].elements[0].focus();
}
}
</script>

<body onload=”setFocus()”>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 591 other followers