HTML DOM add() Method
Complete Select Object Reference
Definition and Usage
The add() method is used to add an option to a dropdown list.
Syntax
selectObject.add(option,before)
|
Parameter |
Description |
option |
Required. Specifies the option to add. Must be an
option or optgroup element |
before |
Required. Where to insert the new option (null
indicates that the new option will be inserted at the end of the
list) |
Example
The following example adds a "kiwi" option to the end of the dropdown list:
<html>
<head>
<script type="text/javascript">
function insertOption()
{
var y=document.createElement('option');
y.text='Kiwi'
var x=document.getElementById("mySelect");
try
{
x.add(y,null); // standards compliant
}
catch(ex)
{
x.add(y); // IE only
}
}
</script>
</head>
<body>
<form>
<select id="mySelect">
<option>Apple</option>
<option>Pear</option>
<option>Banana</option>
<option>Orange</option>
</select>
<input type="button" onclick="insertOption()"
value="Insert option" />
</form>
</body>
</html>
|
Try-It-Yourself Demos
Add an
option to the end of a dropdown list
Add an
option before a selected option in a dropdown list
Complete Select Object Reference
|