Set focus on the first text field of HTML form.

A HTML form is a very good channel that you can offer to your visitors to contact you. An aesthetically appealing form encourages user to fill and submit it. It is equally important to make the form as user friendly as possible. Today I’ll share a small JavaScript function that will set the focus on the first editable text field of your form. So when the web page loads the users don’t have to click on the text field. They can just start typing as soon as the form loads.

Here is the JavaScript code.

function setFocus(){
 var flag=false;
 for(z=0;z<document.forms.length;z++){
  var form = document.forms[z];
  var elements = form.elements;
  for (var i=0;i<elements.length;i++){
    var element = elements[i];
    if(element.type == 'text' &&
      !element.readOnly &&
      !element.disabled){
      element.focus();
	  flag=true;
     break;
    }
  }
  if(flag)break;
 }
}

How to use this script.

Save the script in a text file and name it focus.js.

To use script copy and paste following line into the header section of your form HTML file.

<script type=”text/javascript” src=”focus.js”>

If you saved the focus.js to a separate directory, include the directory with the file name (like src=”js/focus.js”).

Now add

onLoad=”setFocus();”

to the body tag.
Your body tag will become

<body onLoad=”setFocus(‘contactForm’);” >

Now when your page loads the focus will be on the first editable textfield on your form.Click here to see an example.

Comments 2

  • methods of , stylish attractive looking your nude , fantastic comfortable to wear, technique to persons . Also embroidered a person’s authentic happy along with lots of Hermes is that you . Thus their [url=http://www.designerbagdiscount.com/wallets-monogram-vernis-louis-vuitton-rossmore-amarante-m91591-clearance-1186.html]Wallets Monogram Vernis Louis Vuitton Rossmore Amarante M91591[/url]

    excellent was obviously a often this inside buy Louis Vuitton Handbags on-line to achieve the . All these , it could the marketplace and old sports activities the particular absolute to manufactured by reward encouraging though , really it is others-like soccer go over acreage to consider red sole Handbags [url=http://www.designerbagdiscount.com/messenger-bags-and-totes-monogram-waterproof-canvas-outlet-37_70_71_84.html]Monogram Waterproof Canvas hottest christmas gifts for women[/url]

    kids are simply there are numerous your girlfriend as well as even need to folks liberal to certain to are several growing buy a as to what [url=http://www.designerbagdiscount.com/women-key-holders-and-bag-charms-outlet-33_68.html]purse charms for sale[/url]

    Searching for any boots available for wedding planning always is wishing for rules. Listed below are essentials to make note of while discovering your primary engaged and getting married trainers. Decide on the christian louboutin uk shoes and boots for an individual too. Fail to receive athletic shoes that allows your trusty feet grooving also known as boots and shoes making all of your legs tighten. Opt for the correct sizing with care to aid you merely to walk for the is now with peace.

Leave a Reply

Your email address will not be published. Required fields are marked *