option

< html‎ | elements
Jump to: navigation, search

option

This article is In Progress.


W3C Recommendation

Summary

Denotes one choice in a select element.


Overview Table

DOM Interface HTMLOptionElement

Examples

This example uses the OPTION element to create individual items in a drop-down list box.

HTML

<!DOCTYPE html>
<html>
 <head>
  <title>Example</title>
 </head>
 <body>
  <select id="car-list" size="1">
   <option value="1">BMW</option>
   <option value="2">PORSCHE</option>
   <option value="3" selected>MERCEDES</option>
  </select>
  <textarea id="text-area"></textarea>
 </body>
</html>

Using the markup of the previous example, this JavaScript example uses the options collection to append the selected item of the list box in a text area.

JavaScript

function appendCar() {
  var carListElement = document.getElementById("car-list");
   document.getElementById("text-area").value +=carListElement.options[carListElement.selectedIndex].text + "\n";
}
document.addEventListener("DOMContentLoaded", appendCar, false);

Notes

You can create new OPTION elements dynamically with the document.createElement method, but you cannot change properties until the new element is added to a SELECT object. Or, you can create fully formed elements by using the Option object, as follows:

 var opt = new Option( 'Text', 'Value', fDefaultSelected ); 

You can add OPTION elements only to a SELECT element that is located in the same window where the OPTION elements are created. Except for background-color and color, style settings applied through the style object for the option element are ignored. In addition, style settings applied directly to individual options override those applied to the containing SELECT element as a whole.

Members

The option object has these types of members:


Events

The option object has these events.

Event Description
onabort Fires when the user aborts the download.
onafterupdate Fires on a databound object after successfully updating the associated data in the data source object.
onbeforecopy Fires on the source object before the selection is copied to the system clipboard.
onbeforeupdate Fires on a databound object before updating the associated data in the data source object.
onblur Fires when the object loses the input focus.
oncellchange Fires when data changes in the data provider.
onchange Fires when the contents of the object or selection have changed.
ondataavailable Fires periodically as data arrives from data source objects that asynchronously transmit their data.
ondatasetchanged Fires when the data set exposed by a data source object changes.
ondatasetcomplete Fires to indicate that all data is available from the data source object.
onerror Fires when an error occurs during object loading.
onerrorupdate Fires on a databound object when an error occurs while updating the associated data in the data source object.
onfilterchange Fires when a visual filter changes state or completes a transition.
oninput Occurs when the text content of an element is changed through the user interface.
onlayoutcomplete Fires when the print or print preview layout process finishes filling the current LayoutRect object with content from the source document.
onload Fires immediately after the client loads the object.
onlosecapture Fires when the object loses the mouse capture.
onpropertychange Fires when a property changes on the object.
onreadystatechange Fires when the state of the object has changed.
onreset Fires when the user resets a form.
onresize Fires when the size of the object is about to change.
onrowenter Fires to indicate that the current row has changed in the data source and new data values are available on the object.
onrowexit Fires just before the data source control changes the current row in the object.
onrowsdelete Fires when rows are about to be deleted from the recordset.
onrowsinserted Fires just after new rows are inserted in the current recordset.
onselect Fires when the current selection changes.
onselectstart Fires when the object is being selected.

 

Methods

The option object has these methods.

Method Description
addBehavior Attaches a behavior to the element.

This method is not supported for Metro style apps using JavaScript.

appendChild Appends an element as a child to the object.
applyElement Makes the element either a child or parent of another element.
attachEvent Binds the specified function to an event, so that the function gets called whenever the event fires on the object.
clearAttributes Removes all attributes and values from the object.
click Simulates a click by causing the onclick event to fire.
cloneNode Copies a reference to the object from the document hierarchy.
compareDocumentPosition Compares the position of two nodes in a document.
componentFromPoint Returns the component located at the specified coordinates via certain events.
contains Checks whether the given element is contained within the object.
detachEvent Unbinds the specified function from the event, so that the function stops receiving notifications when the event fires.
doScroll Simulates a click on a scroll bar component.
dragDrop Initiates a drag event.
fireEvent Fires a specified event on the object.
getAdjacentText Returns the adjacent text string.
getAttribute Retrieves the value of the specified attribute.
getAttributeNode Retrieves an attribute object referenced by the attribute.name property.
getAttributeNodeNS Gets an attribute object that matches the specified namespace and name.
getAttributeNS Gets the value of the specified attribute within the specified namespace.
getBoundingClientRect Retrieves an object that specifies the bounds of a collection of TextRectangle objects.
getClientRects Retrieves a collection of rectangles that describes the layout of the contents of an object or range within the client. Each rectangle describes a single line.
getElementsByClassName Gets a collection of objects that are based on the value of the CLASS attribute.
getElementsByTagNameNS Gets a collection of objects that are based on the specified element names within a specified namespace.
hasAttribute Determines whether an attribute with the specified name exists.
hasAttributeNS Determines whether an attribute that has the specified namespace and name exists.
hasAttributes Determines whether one or more attributes exist for the object.
hasChildNodes Returns a value that indicates whether the object has children.
insertAdjacentElement Inserts an element at the specified location.
insertAdjacentHTML Inserts the given HTML text into the element at the location.
insertAdjacentText Inserts the given text into the element at the specified location.
insertBefore Inserts an element into the document hierarchy as a child node of a parent object.
isDefaultNamespace Indicates whether or not a namespace is the default namespace for a document.
isEqualNode Determines if two nodes are equal.
isSameNode Determines if two node references refer to the same node.
isSupported Returns a value indicating whether or not the object supports a specific DOM standard.
lookupNamespaceURI Gets the URI of the namespace associated with a namespace prefix, if any.
lookupPrefix Gets the namespace prefix associated with a URI, if any.
mergeAttributes Copies all read/write attributes to the specified element.
msMatchesSelector Determines whether an object matches the specified selector.
normalize Merges adjacent DOM objects to produce a normalized document object model.
releaseCapture Removes mouse capture from the object in the current document.
removeAttribute Removes an attribute from an object.
removeAttributeNode Removes an attribute object from the object.
removeAttributeNS Removes the specified attribute from the object.
removeBehavior Detaches a behavior from the element.
removeChild Removes a child node from the object.
removeNode Removes the object from the document hierarchy.
replaceAdjacentText Replaces the text adjacent to the element.
replaceChild Replaces an existing child element with a new child element.
replaceNode Replaces the object with another element.
setActive Sets the object as active without setting focus to the object.
setAttribute Sets the value of the specified attribute.
setAttributeNode Sets an attribute object node as part of the object.
setAttributeNodeNS Sets an attribute object as part of the object.
setAttributeNS Sets the value of the specified attribute within the specified namespace.
setCapture Sets the mouse capture to the object that belongs to the current document.
swapNode Exchanges the location of two objects in the document hierarchy.

 

Properties

The option object has these properties.

Property Description
attributes Retrieves a collection of attributes of the object.
canHaveChildren Gets a value indicating whether the object can contain child objects.
canHaveHTML Retrieves the value indicating whether the object can contain rich HTML markup.
childElementCount Retrieves the number of immediate child nodes of the current element or a zero if the element does not contain any child nodes. childElementCount does not return all child nodes, only child nodes that are nodeType =1, or element nodes.
className Sets or retrieves the class of the object.
clientHeight Retrieves the height of the object including padding, but not including margin, border, or scroll bar.
clientLeft Retrieves the distance between the offsetLeft property and the true left side of the client area.
clientTop Retrieves the distance between the offsetTop property and the true top of the client area.
clientWidth Retrieves the width of the object including padding, but not including margin, border, or scroll bar.
constructor Returns a reference to the constructor of an object.
dataFormatAs Sets or retrieves the rendering format for the data supplied to the object.
dataSrc Sets or retrieves the source of the data for data binding.
defaultSelected Sets or retrieves the status of an option.
dir Sets or retrieves the reading order of the object.
disabled Sets or retrieves a value that indicates whether the user can interact with the object.
firstChild Gets a reference to the first child in the childNodes collection of the object.
firstElementChild Retrieves a reference to the first child element, or null if there are no child elements.
form Retrieves a reference to the form that the object is embedded in.
id Sets or retrieves the string identifying the object.
index Sets or retrieves the ordinal position of an option in a list box.
innerHTML Sets or retrieves the HTML between the start and end tags of the object.
innerText Sets or retrieves the text between the start and end tags of the object.
isContentEditable Gets the value that indicates whether the user can edit the contents of the object.
isDisabled Gets the value that indicates whether the user can interact with the object.
isMultiLine Retrieves the value indicating whether the content of the object contains one or more lines.
isTextEdit Retrieves whether a TextRange object can be created using the object.
label Sets or retrieves a value that you can use to implement your own label functionality for the object.
lang Sets or retrieves the language to use.
language Sets or retrieves the language in which the current script is written.
lastChild Gets a reference to the last child in the childNodes collection of an object.
lastElementChild Retrieves a reference to the last child element or null if there are no child elements.
localName Retrieves the local name of the fully qualified XML declaration for a node.
namespaceURI Retrieves the namespace URI of the fully qualified XML declaration for a node.
nextElementSibling Retrieves a reference to the sibling element that immediately follows or null if the element does not have any sibling elements that follow it.
nextSibling Retrieves a reference to the next child of the parent for the object.
nodeName Gets the name of a particular type of node.
nodeType Retrieves the type of the requested node.
nodeValue Gets or sets the value of a node.
offsetHeight Retrieves the height of the object relative to the layout or coordinate parent, as specified by the offsetParent property.
offsetLeft Retrieves the calculated left position of the object relative to the layout or coordinate parent, as specified by the offsetParent property.
offsetParent Retrieves a reference to the container object that defines the offsetTop and offsetLeft properties of the object.
offsetTop Retrieves the calculated top position of the object relative to the layout or coordinate parent, as specified by the offsetParent property.
offsetWidth Retrieves the width of the object relative to the layout or coordinate parent, as specified by the offsetParent property.
outerHTML Sets or retrieves the object and its content in HTML.
outerText Sets or retrieves the text of the object.
ownerDocument Retrieves the document object associated with the node.
parentElement Retrieves the parent object in the object hierarchy.
parentNode Retrieves the parent object in the document hierarchy.
parentTextEdit Retrieves the container object in the document hierarchy that can be used to create a TextRange containing the original object.
prefix Retrieves the local name of the fully qualified XML declaration for a node.
previousElementSibling Retrieves a reference to the immediately preceding sibling element or null if the element does not have any preceding siblings.
previousSibling Gets a reference to the previous child of the parent for the object.
readyState Retrieves the current state of the object.
recordNumber Retrieves the ordinal record from the data set that generated the object.
role Sets or retrieves the role for this element.
scopeName Gets the namespace defined for the element.

This property is not supported for Metro style apps using JavaScript.

scrollHeight Retrieves the scrolling height of the object.
scrollLeft Sets or retrieves the distance between the left edge of the object and the leftmost portion of the content currently visible in the window.
scrollTop Sets or retrieves the distance between the top of the object and the topmost portion of the content currently visible in the window.
scrollWidth Retrieves the scrolling width of the object.
selected Sets or retrieves whether the option in the list box is the default item.
sourceIndex Retrieves the ordinal position of the object, in source order, as the object appears in the document's all collection.
STYLE Sets an inline style for the element.
tagName Retrieves the tag name of the object.
tagUrn Sets or gets the URN specified in the namespace declaration.

This property is not supported for Metro style apps using JavaScript.

text Sets or retrieves the text string specified by the option tag.
textContent Sets or retrieves the text content of an object and any child objects.
title Sets or retrieves advisory information (a ToolTip) for the object.
uniqueID Retrieves an autogenerated, unique identifier for the object.
uniqueNumber Retrieves the element's unique number.
value Sets or retrieves the value which is returned to the server when the form control is submitted.

Related specifications

Specification Status Related Changes
HTML 4.01 W3C Recommendation
HTML5 Last Call Working Draft
WHATWG HTML Living Standard

Compatibility

Desktop

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support ? ? ? ? ?

Mobile

Feature Android BlackBerry Chrome for mobile Firefox Mobile (Gecko) IE Mobile Opera Mobile Opera Mini Safari Mobile
Basic support ? ? ? ? ? ? ? ?


Compatibility notes

Browser Version Note
WebKit, Blink (Chrome, Safari and more) Any No support for event handlers.
Internet Explorer Earlier than 5 document.all excludes option elements. Instead, you can use the options collection property of the select element to get them.

See also

Related articles

HTML


















































… further results

Attribution

This article contains content originally from external sources.

Portions of this content come from the Microsoft Developer Network: [Windows Internet Explorer API reference Article]