The Options Collection

Note : The Options Collection is supported by both Netscape and Internet Explorer.

The Options collection is an ordered, indexed array, containing a reference to every <OPTION> element in a given <SELECT> element. A separate options collection is available for each different <SELECT> element in the document and the options collection can only be referenced through a reference to a valid <SELECT> element object.

Option Objects would normally be retrieved by their index in the options collection. I.e.:


returns the VALUE attribute of the second <OPTION> element in the <SELECT> element referenced by <SELECT>reference. For example, consider the following:

<OPTION VALUE="BuryLove" ID="BLove">Bury Your Love Like Treasure
<OPTION VALUE="JacketHangs" ID="JHangs">Jacket Hangs
<OPTION VALUE="LoverAndConfidante" ID="Lover">Lover And Confidante

Calling the following in a script function:


would return JacketHangs.

A string value can be used however, as long as that string is a valid identifier (ID attribute value) for an <OPTION> element in the referenced <SELECT> element in the document.



would return the Lover And Confidante - the displayed text for the <OPTION> element whose ID attribute is 'Lover'.


The length property returns the number of options in the collection. Note that the length count starts at 1, not 0 as the options collection index does. Therefore, the length property may return a value of 5, but to access the 3rd option in a <SELECT>, you'd need to use document.<SELECT>reference.options(2).property


The add method can be used to add option objects to the collection. Note that the new <OPTION> element must first be created by using the createElement method of the Document Object.

The add method takes the single argument of index which specifies where in the options collection the new element is to be placed. If no index argument is specified, the new option object is appended to the end of the collection.

The item method retrieves single items, or sub-collections from an areas collection. It accepts the following arguments:

options.item(index, sub-index)

If index is a number, then the method returns a reference to the <OPTION> at that position in the options collections index. I.e.


would make strTag be the value of the VALUE attribute of the <SELECT>'s third <OPTION>. As you can see, this is effectively the long-hand version of using document.<SELECT>reference.options(2).property.

If the index property is a string value, then the item method returns a sub-collection, containing a reference to every option object in the select object that has its NAME or ID attribute set to the string contained in the index argument. To retrieve certain element objects from this sub-collection, the sub-index argument must be used.

The remove method can be used to remove an option object from a select object. For example, using the <SELECT>/<OPTION> set from above, using:


would remove the second <OPTION> choice, compressing the options collection. Note that removal of option objects can also be achieved by setting their value property to "" (i.e. null).