X
HOME HTML5 CSS3 JAVASCRIPT SQL PHP5 JQUERY ANGULAR BOOTSTRAP3 XML MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

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

Internet Explorer Firefox Opera Google Chrome Safari

The add() method is supported in all major browsers.


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

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook