Latest web development tutorials

HTML DOM Select add () method

Select Object Reference Select Objects

Definition and Usage

add () method is used to <select> Add a <option> element.

grammar

selectObject.add(option,before)

参数 描述
option 必需。要添加选项元素。必需是 option 或 optgroup 元素。
before 必需。在选项数组的该元素之前增加新的元素。如果该参数是null,元素添加到选项数组的末尾。


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support add () method

Tip: add () method requires page statement in IE8 and later versions of IE DOCTYPE!.


Examples

Examples

The following example can be added to a "kiwi" option to the end of the drop-down list:

<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "utf-8">
<Title> This tutorial (w3big.com) </ title>
<Script>
function displayResult () {
var x = document.getElementById ( "mySelect");
var option = document.createElement ( "option");
option.text = "Kiwi";
try {
// For earlier versions of IE8
x.add (option, x.options [null]);
} Catch (e) {
x.add (option, null);
}
}
</ Script>
</ Head>
<Body>

<Form>
<Select id = "mySelect">
<Option> Apple </ option>
<Option> Pear </ option>
<Option> Banana </ option>
<Option> Orange </ option>
</ Select>
</ Form>
<br>
<Button type = "button" onclick = "displayResult ()"> Insert Options </ button>
<P> <b> Note: </ b> add () method works fine in IE8 or later, to add a DOCTYPE declaration in the page!. Also note that the extra code to the previous version of IE 8. </ P>
</ Body>
</ Html>

try it"


Examples

More examples

Before the drop-down list of the selected option to add options


Select Object Reference Select Objects