THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML

Select add() Method

Select Object Reference Select Object

Example

Add a "Kiwi" option at the end of a drop-down list:

var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option);
Try it Yourself »

Definition and Usage

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

Tip: To remove an option from a drop-down list, use the remove() method.


Browser Support

Method
add() Yes Yes Yes Yes Yes

Syntax

selectObject.add(option,index)

Parameter Values

Parameter Description
option Required. Specifies the option to add. Must be an option or optgroup element
index Optional. An integer that specifies the index position for where the new option element should be inserted. Index starts at 0. If no index is specified, the new option will be inserted at the end of the list

Technical Details

Return Value: No return value

More Examples

Example

Add a "Kiwi" option at the beginning of a drop-down list:

var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[0]);
Try it Yourself »

Example

Add a "Kiwi" option at index position "2" of a drop-down list:

var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[2]);
Try it Yourself »

Example

Add an option before a selected option in a drop-down list:

var x = document.getElementById("mySelect");
if (x.selectedIndex >= 0) {
    var option = document.createElement("option");
    option.text = "Kiwi";
    var sel = x.options[x.selectedIndex];
    x.add(option, sel);
}
Try it Yourself »

Select Object Reference Select Object