Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
This chapter is from the book

Scripting Form Elements

The most important property of the form object is the elements array, which contains an object for each of the form elements. You can refer to an element by its own name or by its index in the array. For example, the following two expressions both refer to the first element in the order form, the name1 text field:



Both forms and elements can be referred to by their own names or as indices in the forms and elements arrays. For clarity, the examples in this hour use individual form and element names rather than array references. You'll also find it easier to use names in your own scripts.

If you do refer to forms and elements as arrays, you can use the length property to determine the number of objects in the array: document.forms.length is the number of forms in a document, and document.form1.elements.length is the number of elements in the form1 form.

Text Fields

Probably the most commonly used form elements are text fields. You can use them to prompt for a name, address, or any information. With JavaScript, you can display text in the field automatically. The following is an example of a simple text field:

<input TYPE="TEXT" NAME="text1" VALUE="hello" size="30">

This defines a text field called text1. The field is given a default value of "hello" and allows up to 30 characters to be entered. JavaScript treats this field as a text object with the name text1.

Text fields are the simplest to work with in JavaScript. Each text object has the following properties:

  • name is the name given to the field. This is also used as the object name.

  • defaultValue is the default value and corresponds to the VALUE attribute. This is a read-only property.

  • value is the current value. This starts out the same as the default value, but can be changed, either by the user or by JavaScript functions.

When you work with text fields, most of the time you will use the value attribute to read the value the user has entered or to change the value. For example, the following statement changes the value of a text field called username in the order form to "John Q. User":

document.order.username.value = "John Q. User"

Text Areas

Text areas are defined with their own tag, <textarea>, and are represented by the textarea object. There is one major difference between a text area and a text field: Text areas allow the user to enter more than just one line of information. Here is an example of a text area definition:

<textarea NAME="text1" ROWS="2" COLS="70">
This is the content of the TEXTAREA tag.

This HTML defines a text area called text1, with two rows and 70 columns available for text. In JavaScript, this would be represented by a text area object called text1 under the form object.

The text between the opening and closing <textarea> tags is used as the initial value for the text area. You can include line breaks within the default value.

Working with Text in Forms

The text and textarea objects also have a few methods you can use:

  • focus() sets the focus to the field. This positions the cursor in the field and makes it the current field.

  • blur() is the opposite; it removes the focus from the field.

  • select() selects the text in the field, just as a user can do with the mouse. All of the text is selected; there is no way to select part of the text.

You can also use event handlers to detect when the value of a text field changes. The text and textarea objects support the following event handlers:

  • The onFocus event happens when the text field gains focus.

  • The onBlur event happens when the text field loses focus.

  • The onChange event happens when the user changes the text in the field and then moves out of it.

  • The onSelect event happens when the user selects some or all of the text in the field. Unfortunately, there's no way to tell exactly which part of the text was selected. (If the text is selected with the select() method described above, this event is not triggered.)

If used, these event handlers should be included in the <input> tag declaration. For example, the following is a text field including an onChange event that displays an alert:

<input TYPE="TEXT" NAME="text1" onChange="window.alert('Changed.');">


The final type of form element is a button. Buttons use the <input> tag and can use one of three different types:

  • type=SUBMIT is a submit button. This button causes the data in the form fields to be sent to the CGI script.

  • type=RESET is a reset button. This button sets all the form fields back to their default value, or blank.

  • type=BUTTON is a generic button. This button performs no action on its own, but you can assign it one using a JavaScript event handler.

All three types of buttons include a NAME attribute to identify the button and a VALUE that indicates the text to display on the button's face. A few buttons were used in the examples in Hour 11, "Using Windows and Frames." As another example, the following defines a Submit button with the name sub1 and the value "Click Here":

<input TYPE="SUBMIT" NAME="sub1" VALUE="Click Here">

If the user presses a Submit or Reset button, you can detect it with the onSubmit or onReset event handlers, described earlier in this hour. For generic buttons, you can use an onClick event handler.

Check Boxes

A check box is a form element that looks like a small box. Clicking on the check box switches between the checked and unchecked states, which is useful for indicating Yes or No choices in your forms. You can use the <input> tag to define a check box. Here is a simple example:


Again, this gives a name to the form element. The VALUE attribute assigns a meaning to the check box; this is a value that is returned to the server if the box is checked. The default value is "on." The CHECKED attribute can be included to make the box checked by default.

A check box is simple: it has only two states. Nevertheless, the checkbox object in JavaScript has four different properties:

  • name is the name of the check box, and also the object name.

  • value is the "true" value for the check box—usually on. This value is used by the server to indicate that the check box was checked. In JavaScript, you should use the checked property instead.

  • defaultChecked is the default status of the check box, assigned by the CHECKED attribute.

  • checked is the current value. This is a boolean value: true for checked and false for unchecked.

To manipulate the check box or use its value, you use the checked attribute. For example, this statement turns on a check box called same in the order form:

document.order.same.checked = true;

The check box has a single method, click(). This method simulates a click on the box. It also has a single event, onClick, which occurs whenever the check box is clicked. This happens whether the box was turned on or off, so you'll need to examine the checked property to see what happened.

Radio Buttons

Another element for decisions is the radio button, using the <input> tag's RADIO type. Radio buttons are also known as option buttons. These are similar to check boxes, but they exist in groups and only one button can be checked in each group. They are used for a multiple-choice or "one of many" input. Here's an example of a group of radio buttons:

<input TYPE="RADIO" NAME="radio1" VALUE="Option1" CHECKED> Option 1
<input TYPE="RADIO" NAME="radio1" VALUE="Option2"> Option 2
<input TYPE="RADIO" NAME="radio1" VALUE="Option3"> Option 3

These statements define a group of three radio buttons. The NAME attribute is the same for all three (which is what makes them a group). The VALUE attribute is the value passed to a script or CGI program to indicate which button is selected—be sure you assign a different value to each button.


Radio buttons are named for their similarity to the buttons on old pushbutton radios. Those buttons used a mechanical arrangement so that when you pushed one button in, the others popped out.

As for scripting, radio buttons are similar to check boxes, except that an entire group of them shares a single name and a single object. You can refer to the following properties of the radio object:

  • name is the name common to the radio buttons.

  • length is the number of radio buttons in the group.

To access the individual buttons, you treat the radio object as an array. The buttons are indexed, starting with 0. Each individual button has the following properties:

  • value is the value assigned to the button. (This is used by the server.)

  • defaultChecked indicates the value of the CHECKED attribute and the default state of the button.

  • checked is the current state.

For example, you can check the first radio button in the radio1 group on the form1 form with this statement:

document.form1.radio1[0].checked = true;

However, if you do this, be sure you set the other values to false as needed. This is not done automatically. You can use the click method to do both of these in one step.

Like a check box, radio buttons have a click() method and an onClick event handler. Each radio button can have a separate statement for this event.

Drop-Down Lists

A final form element is also useful for multiple-choice selections. The <select> HTML tag is used to define a selection list, or a drop-down list of text items. The following is an example of a selection list:

<select NAME="select1" SIZE=40>
<option VALUE="choice1" SELECTED>This is the first choice.
<option VALUE="choice2">This is the second choice.
<option VALUE="choice3">This is the third choice.

Each of the <option> tags defines one of the possible choices. The VALUE attribute is the name that is returned to the program, and the text outside the <option> tag is displayed as the text of the option.

An optional attribute to the <select> tag, MULTIPLE, can be specified to allow multiple items to be selected. Browsers usually display a single-selection <select> as a drop-down list and a multiple-selection list as a scrollable list.

The object for selection lists is the select object. The object itself has the following properties:

  • name is the name of the selection list.

  • length is the number of options in the list.

  • options is the array of options. Each selectable option has an entry in this array.

  • selectedIndex returns the index value of the currently selected item. You can use this to check the value easily. In a multiple-selection list, this indicates the first selected item.

The options array has a single property of its own, length, which indicates the number of selections. In addition, each item in the options array has the following properties:

  • index is the index into the array.

  • defaultSelected indicates the state of the SELECTED attribute.

  • selected is the current state of the option. Setting this property to true selects the option. You can select multiple options if the MULTIPLE attribute is included in the <select> tag.

  • name is the value of the NAME attribute. This is used by the server.

  • text is the text that is displayed in the option. In Netscape 3.0 or later, you can change this value.

The select object has two methods—blur() and focus()—which perform the same purposes as the corresponding methods for text objects. The event handlers are onBlur, onFocus, and onChange, also similar to other objects.


You can change selection lists dynamically—for example, choosing a product in one list could control which options are available in another list. You can also add and delete options from the list.

Reading the value of a selected item is a two-step process. You first use the selectedIndex property, then use the value property to find the value of the selected choice. Here's an example:

ind = document.navform.choice.selectedIndex;
val = document.navform.choice.options[ind].value;

This uses the ind variable to store the selected index, then assigns the val variable to the value of the selected choice. You'll see an example script that uses this technique in Hour 21, "Improving a Web Page with JavaScript."

  • + Share This
  • 🔖 Save To Your Account