Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
HTML DOM Select add() Method
The HTML DOM select add() method is used to add a new option element to an HTML <select> dropdown list. This method dynamically inserts options into existing select lists, making it useful for creating interactive forms where options are added based on user actions or other conditions.
Syntax
Following is the syntax for the select add() method −
selectObject.add(option, index)
Parameters
The add() method accepts the following parameters −
option − An HTMLOptionElement object or HTMLOptGroupElement object to be added to the select list.
index (optional) − An integer specifying the position where the new option should be inserted. If omitted, the option is added at the end of the list.
Return Value
The add() method does not return any value. It directly modifies the select element by adding the new option.
Adding Option at the End
Following example demonstrates adding a new option to the end of a dropdown list −
<!DOCTYPE html>
<html>
<head>
<title>Select add() Method - End Position</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Add Option to Dropdown</h2>
<p>Select your favorite subject:</p>
<select id="subjectList" style="padding: 5px; font-size: 16px;">
<option value="physics">Physics</option>
<option value="biology">Biology</option>
<option value="chemistry">Chemistry</option>
</select>
<br><br>
<button onclick="addHistory()" style="padding: 8px 16px; font-size: 14px;">Add History</button>
<p id="message"></p>
<script>
function addHistory() {
var selectList = document.getElementById("subjectList");
var newOption = document.createElement("option");
newOption.value = "history";
newOption.text = "History";
selectList.add(newOption);
document.getElementById("message").innerHTML = "History option added successfully!";
}
</script>
</body>
</html>
The output shows a dropdown with three subjects initially, and clicking the button adds History as the fourth option −
Select your favorite subject: [Physics ?] [Add History] After clicking: [Physics ?] with History, Chemistry, Biology, Physics options History option added successfully!
Adding Option at Specific Position
Following example shows how to add an option at a specific index position −
<!DOCTYPE html>
<html>
<head>
<title>Select add() Method - Specific Position</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Add Option at Position 1</h2>
<p>Programming languages:</p>
<select id="langList" style="padding: 5px; font-size: 16px;">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="python">Python</option>
</select>
<br><br>
<button onclick="addJavaScript()" style="padding: 8px 16px; font-size: 14px;">Insert JavaScript at Position 1</button>
<p id="status"></p>
<script>
function addJavaScript() {
var selectList = document.getElementById("langList");
var newOption = document.createElement("option");
newOption.value = "javascript";
newOption.text = "JavaScript";
// Add at index 1 (second position)
selectList.add(newOption, 1);
document.getElementById("status").innerHTML = "JavaScript inserted at position 1!";
}
</script>
</body>
</html>
The JavaScript option is inserted at index 1, making it the second option in the list −
Programming languages: [HTML ?] [Insert JavaScript at Position 1] After clicking: Options become HTML, JavaScript, CSS, Python JavaScript inserted at position 1!
Multiple Options with Loop
Following example demonstrates adding multiple options using a loop −
<!DOCTYPE html>
<html>
<head>
<title>Select add() Method - Multiple Options</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Add Multiple Colors</h2>
<p>Available colors:</p>
<select id="colorList" style="padding: 5px; font-size: 16px;">
<option value="red">Red</option>
<option value="blue">Blue</option>
</select>
<br><br>
<button onclick="addColors()" style="padding: 8px 16px; font-size: 14px;">Add More Colors</button>
<p id="result"></p>
<script>
function addColors() {
var selectList = document.getElementById("colorList");
var newColors = ["Green", "Yellow", "Purple", "Orange"];
for (var i = 0; i < newColors.length; i++) {
var newOption = document.createElement("option");
newOption.value = newColors[i].toLowerCase();
newOption.text = newColors[i];
selectList.add(newOption);
}
document.getElementById("result").innerHTML = "Added " + newColors.length + " more colors!";
}
</script>
</body>
</html>
This adds four new color options to the existing dropdown list −
Available colors: [Red ?] [Add More Colors] After clicking: Dropdown contains Red, Blue, Green, Yellow, Purple, Orange Added 4 more colors!
Key Points
The add() method requires an HTMLOptionElement object created using
document.createElement("option").If no index is specified, the new option is added at the end of the list.
Index values start from 0. Using an invalid index throws an error.
The method modifies the original select element and does not return a value.
Both
valueandtextproperties should be set for proper option functionality.
Browser Compatibility
The select add() method is supported in all modern browsers including Chrome, Firefox, Safari, Edge, and Internet Explorer 8+. It is part of the standard DOM specification and has consistent behavior across different platforms.
Conclusion
The HTML DOM select add() method provides a straightforward way to dynamically add options to dropdown lists. It accepts an option element and an optional index parameter, allowing precise control over where new options are inserted in the select list.
