0

Adding field focus to every form on your site

ColdFusion
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()">
tags:
ColdFusion

Search