Search w3schools.com:

SHARE THIS PAGE

Select add() Method

Select Object Reference Select Object

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)


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

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

Tip: For this method to work in IE8 and higher, add a !DOCTYPE declaration to the page. Also notice the extra code for IE prior version 8.


Example

Example

Insert a "Kiwi" option at the end of the dropdown list:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
function displayResult()
{
var x=document.getElementById("mySelect");
var option=document.createElement("option");
option.text="Kiwi";
try
  {
  // for IE earlier than version 8
  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>

<button type="button" onclick="displayResult()">Insert option</button>

</body>
</html>

Try it yourself »


Examples

More Examples

Add an option before a selected option in a dropdown list


Select Object Reference Select Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]