Option object
Creates and initializes a new option element.
Members
The Option object has these types of members:
Methods
The Option object has these methods.
| Method | Description |
|---|---|
| create |
Initializes a new option element. This method cannot be called directly. See Option object. |
Standards information
There are no standards that apply here.
Remarks
Use this object to instantiate new option elements before adding them to a select element. You can specify up to four optional arguments:
| sText | String that specifies the option text. |
| sValue | String that specifies the option value. |
| bDefaultSelected | Boolean that indicates whether the option is the default selection. |
| bSelected | Boolean that indicates whether this option is selected when it is added to the collection. |
Numeric values are coerced into string and Boolean equivalents if possible.
Examples
The following script creates three new option objects and adds them to a select element. Because of the optional arguments, option "Two" is originally selected but option "Three" gains focus when the reset button is pressed.
<form action="#"> <select id="oSelect"></select> <button type="reset">Reset to Defaults</button> </form> <script type="text/javascript"> var sel = document.getElementById('oSelect'); sel.options.add( new Option("One","1") ); sel.options.add( new Option("Two","2",false,true) ); sel.options.add( new Option("Three",3,1,0) ); </script>
See also
Show: