Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

Option

Inherits from HTMLOptionElementHTMLOptionElement

Properties

No properties.

Methods

No methods.

Events

No events.

Inherited from HTMLOptionElement

Properties

defaultSelected
Gets or sets the value of the selected HTML attribute.

Methods

No methods.

Events

No events.

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>

Notes

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.

Related specifications

WHATG HTML
Living Standard
HTML5
Last Call Working Draft

Attributions