optionsNN 2 IE 3 DOM 1  

  

An array of option elements nested within a select object.

 
Object Model Reference
 
[window.]document.formName.selectName.options
[window.]document.forms[i].elements[i].options
[window.]document.getElementById("selectElementID").options
 
Object-Specific Properties
 
length
 
Object-Specific Methods
 
add( )item( )namedItem( )remove( )tags( )urns( )
lengthNN 2 IE 3 DOM 1  

Read-only  

Returns the number of elements in the collection.

 
Example
 
var howMany = document.forms[0].mySelect.options.length;
 
Value

Integer.

add( )NN n/a IE 4 DOM n/a  

add(elementRef[, index])

  

Adds an already-created element (from the createElement( ) method) to the current collection. The element must be of the option type. By default, the new element is added as the last item of the collection unless you specify an index value as a second parameter (in which case all existing items from that index position get pushed down by one). The following example sequence appends a new item to a select object:

var newElem = document.createElement("option");
newElem.text = "Freddy";
newElem.value = "Freddy Mercury";
document.forms[1].rockers.options.add(newElem);
 

Notice that a generic object is created first. Then its properties are stuffed with values, and the new element is added to the select element.

 

For an example of a cross-browser and backward-compatible approach to this task, see the option object discussion. Also see the select.add( ) method for a W3C DOM approach that works with Netscape 6.

 
Parameters
 
  • A fully-formed element object reference, usually generated by the createElement( ) method.
  • An optional integer indicating where in the collection the new element should be placed.
 
Returned Value

None.

item( )NN 6 IE 4 DOM 1  

item(index[, subindex]) item(index)

  

Returns a single object or collection of objects corresponding to the element matching the index value (or, optionally, the index and subindex values).

 
Parameters
 
  • When the parameter is a zero-based integer, the returned value is a single element corresponding to the specified item in source code order (nested within the current element); when the parameter is a string (IE only), the returned value is a collection of elements with id properties that match that string.
  • In IE only, if you specify a string value for the first parameter, you can use the second parameter to specify a zero-based index that retrieves the specified element from the collection with id properties that match the first parameter's string value.
 
Returned Value

One object or collection (array) of objects. If there are no matches to the parameters, the returned value is null.

namedItem( )NN 6 IE 6 DOM 1  

namedItem("ID")

  

Returns a single option object corresponding to the element matching the parameter string value.

 
Parameters
 
  • The string that contains the same value as the desired element's id attribute.
 
Returned Value

One option object. If there are no matches to the parameters, the returned value is null.

remove( )NN n/a IE 4 DOM n/a  

remove(index)

  

Deletes an element from the current collection. Simply specify the zero-based index value of the option element you wish to remove from the collection belonging to a select element. The following example deletes the first item from a select object:

document.forms[1].rockers.options.remove(0);
 

The process for removing an option element is entirely different in Navigator. To delete an item, assign null to the item in the collection. For example, the Navigator version of the preceding IE example is as follows:

document.forms[1].rockers.options[0] = null;
 

Regardless of the browser-specific process of removing an option from the select object, the length of the options array collapses to fill the space.

 
Parameters
 
  • A zero-based integer indicating which item in the collection should be deleted.
 
Returned Value

None.

tags( )NN n/a IE 4 DOM n/a  

tags("tagName")

  

Returns a collection of objects (among all objects nested within the current collection) with tags that match the tagName parameter. Implemented in all IE collections (see the all.tags( ) method), but redundant for collections of the same element type.

urns( )NN n/a IE 5(Win) DOM n/a  

urns(URN)

  

See the all.urns( ) method.