JavaScript » Objects » Option

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

An Option object is created for every option in a selection list, and is put in the options property of the Select object. It can be created in one of two ways: you can either use the HTML <OPTION> tag, or use the Option constructor.

The selected argument is used for multiple selections.

Examples

Code:
<option> Dachshund
Explanation:

This example shows how to use HTML to create a 'Dachshund' option for a selection list of dog.

Code:
document.myForm.dogBreed[4] = new Option("Dachshund")
Explanation:

You could also create the same option using the Option constructor and assigning it to an index of the options property of the relevent Select object.

Code:
new Option("Dachshund", "dachs")
Explanation:

After creating an Option object in this way you must refresh the document by using history.go(0) at the end of the code. Using the Option constructor, you can optionally specify a value to be returned to the server when an option is selected and the form submitted (in this case "dachs").

Code:
new Option("Dachshund", "dachs", true)
Explanation:

It is also possible to designate the option to be the pre-selected default selection in the option box display (i.e., this option has the HTML "selected" attribute included inside the "option" tag). This is done by setting the defaultSelected argument to be true.

Properties

defaultSelected

Syntax: object.defaultSelected

This property, by default tainted, is a Boolean value which initially reflects whether an option was declared with the HTML SELECTED attribute, reading true if it was and false if not

selected

Syntax: object.selected

This property, which is tainted by default, is a Boolean value reflecting whether a particular option is selected, returning true if it is and false if not. The selected property can be set at any time, immediately updating the display of the Select object.

text

Syntax: object.text

This property, by default tainted, reflects the text value following any particular HTML OPTION tag for a Select object. It can be reset at any time, immediately updating the display of the selection.

value

Syntax: object.value

This property, tainted by default, is a string value that is returned to the server when an option is selected and the form submitted. It reflects the VALUE attribute in the HTML. If there is no VALUE attribute, then the value property is an empty string.

See Also: