JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Option Object

Name

Option Object---an option in a Select box

Availability

Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0

Synopsis

select.options[i]

Properties

defaultSelected

A read-only Boolean that specifies whether this option is selected by default. Set by the SELECTED attribute.

index

A read-only integer that specifies the index of this option within the array of options.

selected

A read/write Boolean that specifies whether this option is currently selected. Its initial value is specified by the SELECTED attribute.

text

The text that describes the option. It is the plain text (not formatted HTML text) that follows the <OPTION> tag. In Navigator 2.0 and Internet Explorer 3.0, this property is read-only. In Navigator 3.0 it is read/write.

value

A read/write string that specifies the value to be passed to the server if this option is selected when the form is submitted. The initial value is specified by the VALUE attribute.

Constructor

new Option([text, [value, [defaultSelected, [selected]]]])

Arguments

text

An optional string argument that specifies the text property of the Option object.

value

An optional string argument that specifies the value property of the Option object.

defaultSelected

An optional Boolean argument that specifies the defaultSelected property of the Option object.

selected

An optional Boolean argument that specifies the selected property of the Option object.

Returns

The newly created Option object.

HTML Syntax

An Option object is created by an <OPTION> tag within a <SELECT> which is itself within a <FORM>. Multiple <OPTION> tags typically appear within the <SELECT>.

<FORM ...>
  <SELECT  ...>
    <OPTION 
        [ VALUE="value" ] the value returned when the form is submitted
        [ SELECTED ] >   specifies whether this option is initially selected
    plain_text_label  the text to display for this option
        ...
  </SELECT>
        ...
</FORM>

Description

The Option object describes a single option displayed within a Select object. The properties of this object specify whether it is selected by default, whether is is currently selected, what position it has in the options[] array of its containing Select object, what text it displays, and what value it passes to the server if it is selected when the containing form is submitted.

Note that although the text displayed by this option is specified outside of the <OPTION> tag, that text must be plain, unformatted text, without any HTML tags. This is so that the text can be properly displayed in list boxes and drop-down menus that do not support HTML formatting.

You can dynamically create new Option objects for display in a Select object with the Option() constructor. Once a new Option object is created, it can be appended to the list of options in a Select object by assigning it to the variable options[options.length]. See Select.options[] for details.

See Also

"Select", "Select.options[]"


Previous Home Next
opener Book Index Option.defaultSelected

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell