HTMLOptionsCollection: add() method

The add() method of the HTMLOptionsCollection interface adds an HTMLOptionElement or HTMLOptGroupElement to this HTMLOptionsCollection.

Syntax

js
add(item)
add(item, before)

Parameters

item

An HTMLOptionElement or HTMLOptGroupElement.

before Optional

An element of the collection, or a numeric 0-based index representing the element that the item should be inserted before. If omitted, null, or the index does not exist, the new element is appended to the end of the collection.

Return value

None (undefined).

Exceptions

HierarchyRequestError DOMException

Thrown if the item passed to the method is an ancestor of the element into which it is to be inserted.

Description

By default, the add() appends the <option> or <optgroup> passed as the parameter to the end of the collection. You can define where the added <option> or <optgroup> should be placed by specifying the before parameter. The before is the <option> element or a numeric 0-based index of the <option> element the added element should precede.

If the before parameter is null or out of range (or omitted), the <option> or <optgroup> will be appended as the last element in the collection, outside of any <optgroup>. If the <option> referenced by the before parameter is in an <optgroup>, an added HTMLOptionElement will be in the same group.

The <optgroup> element can only contain <option> elements as child nodes. The add() method will successfully add an HTMLOptGroupElement to the end of the HTMLOptionsCollection or between <optgroup> elements only. In other words, attempting to add an HTMLOptGroupElement before an <option> within an <optgroup> may silently fail if the <option> referenced by the before parameter is not the first <option> within its <optgroup>.

Examples

js
const optionList = document.querySelector("select").options;
const firstOption = document.createElement("option");
firstOption.text = "new item";
optionList.add(firstOption, 0); // added as the first item
optionList.add(optionList[0]); // moves the first item to the end

Specifications

Specification
HTML
# dom-htmloptionscollection-add-dev

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
add
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote Full support
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
footnote Full support
Safari – Full support
Safari 3 (Release date: 2007-10-26)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also