Name

HTMLOptionsCollection — a collection of Option elements

Inherits from

HTMLCollection

Synopsis

HTMLOptionsCollection is a specialized HTMLCollection that represents the Option elements within a Select element. It overrides the namedItem() method to handle multiple Option elements with the same name, and it defines methods for adding and removing elements. For historical reasons, HTMLOptionsCollection defines a writable length property that you can set to truncate or extend the collection.

Properties

unsigned long length

This property returns the number of elements in the collection. Unlike the length property of a regular HTMLCollection, however, this one is not read-only. If you set it to a value smaller than its current value, the collection of Option elements is truncated, and those that are no longer in the collection are removed from the containing Select element. If you set length to a value larger than its current value, empty <option/> elements are created and added to the Select element and to the collection.

long selectedIndex

The index of the first selected Option in the collection, or -1 if no Option is selected. You can set this property to change the selected item.

Methods

void add(Element option, [any before])

Insert the option (which must be an <option> or <optgroup> element) into this collection (and into the Select element) at the position specified by before. If before is null, insert it at the end. If before is an integer index, insert it before the item that is currently ...

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.