JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript

First Focus

Join the Discussion

Questions? Comments?

If you have one or more forms on your web page then you might like to make it slightly easier for your visitors to start inputting information into your form by giving the focus to the first input field within the form so that they can start typing straight away.

You can easily give the focus to the first input field by adding the following to the body tag of your page:

onload="x = document.getElementsByTagName('input')[0];
continued from previous lineif (x.value == '') x.focus();"

This code locates the first input field on the page (no matter what you have called it) and gives it the focus so that the keyboard cursor will appear at the start of the input field.

Explore JavaScript

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

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

JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript

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

All rights reserved.