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

W3Schools Certification

W3Schools' Online Certification

The perfect solution for professionals who need to balance work, family, and career building.

More than 10 000 certificates already issued!

Get Your Certificate »

The HTML Certificate documents your knowledge of HTML.

The HTML5 Certificate documents your knowledge of advanced HTML5.

The CSS Certificate documents your knowledge of advanced CSS.

The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.

The jQuery Certificate documents your knowledge of jQuery.

The XML Certificate documents your knowledge of XML, XML DOM and XSLT.

The ASP Certificate documents your knowledge of ASP, SQL, and ADO.

The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]