January 18, 2000 - The Option Constructor
January 18, 2000 The Option Constructor Tips: January 2000
Yehuda Shiran, Ph.D.
|
Option
object is contained by the Select
object. The Select
object may have many options. They are contained in its options[]
array. Here is an example for a reference to an option:
document.forms[3].elements[2].options[5]
The option objects can be create dynamically at runtime, via the Option
constructor. The syntax for creating an option is as follows:
var optionObj = new Option([optionText, optionValue, defaultSelected, selected]);
where:
optionText
is a string representing the option's text
property
optionValue
is a string representing the option's value
property
defaultSelected
is a string representing the option's defaultSelected
property
selected
is a string representing the option's selected
property
You can add an option to an existing select object in the following fashion:
selectman.options[index] = optionObj;
After you create an option and add it to a Select
object, you must refresh the document. One way to refresh the document is by using the following JavaScript statement:
history.go(0)
You can also delete an option from a Select
object, by assigning it a null
value. The general syntax will then be:
selectName.options[index] = null;