1. Home
  2. Computing & Technology
  3. JavaScript
photo of Stephen Chapman
Stephen's JavaScript Blog

By Stephen Chapman, About.com Guide to JavaScript since 2004

First Focus

Saturday September 26, 2009
JavaScript can help your visitors start filling out a form without having to use their mouse first.

What normally happens when you add a form to your web page is that your visitor must first select the first field in the form before they can start typing in their information. A simple addition to the body tag of your page can give the first input field the focus when the page first appears.

First Focus
Comments
May 24, 2006 at 6:36 pm
(1) Dean says:

A problem with this trick and slower connections is that a form will often load and display well before the entire page loads. A user may finish typing into the first field and begin typing in the next field before the page has finished loading. With this script and the above, (common), scenario, focus is taken from the second field while the user is typing into it, ( when the page actually finishes loading).

May 27, 2006 at 7:26 pm
(2) javascript says:

Easily fixed.

I have now changed the code so that it only gives the first field the focus if it is still empty when the page finishes loading.

Of course any page that takes long enough to load that your visitors have time to start filling in a form is probably too big anyway.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.