W3Schools.com

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 type="text/javascript">
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
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$7.95/mo SEO Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Free Website Templates Free CSS Templates
Make Your Own Website
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE