HTMLSelectElement: add()-Methode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Die HTMLSelectElement.add()-Methode fügt ein Element zur Sammlung der option-Elemente für dieses select-Element hinzu.

Syntax

js
add(item)
add(item, before)

Parameter

item

Ein HTMLOptionElement oder HTMLOptGroupElement

before Optional

Ein Element der Sammlung oder ein Index vom Typ long, vor welchem der item eingefügt werden soll. Wenn dieser Parameter null ist (oder der Index nicht existiert), wird das neue Element am Ende der Sammlung angehängt.

Rückgabewert

Keiner (undefined).

Ausnahmen

HierarchyRequestError DOMException

Ausgelöst, wenn das item, das an die Methode übergeben wird, ein Vorfahre des HTMLSelectElement ist.

Beispiele

Elemente von Grund auf neu erstellen

js
const sel = document.createElement("select");
const opt1 = document.createElement("option");
const opt2 = document.createElement("option");

opt1.value = "1";
opt1.text = "Option: Value 1";

opt2.value = "2";
opt2.text = "Option: Value 2";

sel.add(opt1, null);
sel.add(opt2, null);

/*
  Produces the following, conceptually:

  <select>
    <option value="1">Option: Value 1</option>
    <option value="2">Option: Value 2</option>
  </select>
*/

Der before-Parameter ist optional. Daher ist Folgendes akzeptiert.

js
sel.add(opt1);
sel.add(opt2);

An eine bestehende Sammlung anhängen

js
const sel = document.getElementById("existingList");

const opt = document.createElement("option");
opt.value = "3";
opt.text = "Option: Value 3";

sel.add(opt, null);

/*
  Takes the existing following select object:

  <select id="existingList">
    <option value="1">Option: Value 1</option>
    <option value="2">Option: Value 2</option>
  </select>

  And changes it to:

  <select id="existingList">
    <option value="1">Option: Value 1</option>
    <option value="2">Option: Value 2</option>
    <option value="3">Option: Value 3</option>
  </select>
*/

Der before-Parameter ist optional. Daher ist Folgendes akzeptiert.

js
sel.add(opt);

In eine bestehende Sammlung einfügen

js
const sel = document.getElementById("existingList");

const opt = document.createElement("option");
opt.value = "3";
opt.text = "Option: Value 3";

sel.add(opt, sel.options[1]);

/*
  Takes the existing following select object:

  <select id="existingList">
    <option value="1">Option: Value 1</option>
    <option value="2">Option: Value 2</option>
  </select>

  And changes it to:

  <select id="existingList">
    <option value="1">Option: Value 1</option>
    <option value="3">Option: Value 3</option>
    <option value="2">Option: Value 2</option>
  </select>
*/

Spezifikationen

Specification
HTML
# dom-select-add-dev

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
add
Index as before parameter

Legend

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

Full support
Full support