Javascript Reference - HTML DOM Select add() Method








The add() method can add an option to a drop-down list.

Browser Support

add Yes Yes Yes Yes Yes

Syntax

selectObject.add(option, index)

Parameter Values

Parameter Description
option Required. option or optgroup element to add.
index Optional. The index position for new addec option element. Index starts at 0. If no index is specified, the new option is added at the end of the list




Return Value

No return value

Example

The following code shows how to add a "Z" option at the beginning of a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!-- www .  j  a  v a 2 s.c  o m-->
<form>
<select id="mySelect" size="8">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select>
</form>
<button type="button" onclick="myFunction()">Insert option</button>
<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    var option = document.createElement("option");
    option.text = "Z";
    x.add(option, x[0]);
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to add a "Z" option at index position "2" of a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--  w  w  w  .j a  va 2s  .c  om-->
<form>
<select id="mySelect" size="8">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select>
</form>
<br>
<button type="button" onclick="myFunction()">Insert option</button>
<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    var option = document.createElement("option");
    option.text = "Z";
    x.add(option, x[2]);
}
</script>
</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to add an option before a selected option in a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--   w  w w  .ja  va2 s .  c o  m-->
<form>
<select id="mySelect" size="8">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select>
</form>
<button type="button" onclick="myFunction()">Insert option before selected</button>
<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    if (x.selectedIndex >= 0) {
        var option = document.createElement("option");
        option.text = "Z";
        var sel = x.options[x.selectedIndex];  
        x.add(option, sel);
    }
}
</script>

</body>
</html>

The code above is rendered as follows:

Example 4

The following code shows how to add a "Z" option at the end of a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--from w  ww. j  av  a  2s .c  o  m-->
<form>
<select id="mySelect" size="8">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select>
</form>
<br>
<button type="button" onclick="myFunction()">Insert option</button>

<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    var option = document.createElement("option");
    option.text = "Z";
    x.add(option);
}
</script>

</body>
</html>

The code above is rendered as follows: