Select options collection
Definition and usage
options
The collection returns all options in the dropdown list Option element collection.
Note:The elements in the collection are sorted in the order they appear in the source code.
Instances
Example 1
View how many options are in a specific dropdown list:
var x = document.getElementById("mySelect").options.length;
The result of x will be:
4
Tip:More examples are provided below the page.
Syntax
selectObject.options
Property
Property | Description |
---|---|
length |
Return the number of <option> elements in the collection. Note:This property is read-only |
selectedIndex | Set or return the index (starting from 0) of the selected <option> element in the collection. |
Method
Method | Description |
---|---|
[index] |
Return the <option> element with the specified index (starting from 0) from the collection. Note:Returns null if the index number is out of range. |
[add(option[,index]]] | Add the <option> element to the specified index in the collection. If no index is specified, it will be inserted at the end of the collection. |
item(index) |
Return the <option> element with the specified index (starting from 0) from the collection. Note:Returns null if the index number is out of range. |
namedItem(id) |
Return the <option> element with the specified ID from the collection. Note:If id Returns null if not found. |
remove(index) | Remove the <option> element with the specified index from the collection. |
Technical Details
DOM Version: | Core Level 2 Document Object |
---|---|
Return value: |
HTMLOptionsCollection object, representing all <option> elements within the <select> element. The elements in the collection are sorted in the order they appear in the source code. |
More Examples
Example 2: [index]
Get the text of the first option (index 0) in the dropdown list:
var x = document.getElementById("mySelect").options[0].text;
The result of x will be:
Apple
Example 3: item(index)
Get the text of the first option (index 0) in the dropdown list:
var x = document.getElementById("mySelect").options.item(0).text;
The result of x will be:
Apple
Example 4: namedItem(id)
Get the text of the option with id="orange" in the dropdown list:
var x = document.getElementById("mySelect").options.namedItem("orange").text;
The result of x will be:
Orange
Example 5
Add the 'Kiwi' option at the index position '1' in the dropdown list:
var x = document.getElementById("mySelect"); var c = document.createElement("option"); c.text = "Kiwi"; x.options.add(c, 1);
Example 6
Remove the option at index "1" from the dropdown list:
var x = document.getElementById("mySelect"); x.options.remove(1);Example 7
Loop through all options in the dropdown list and output the text of each option:
var x = document.getElementById("mySelect"); var txt = ""; var i; for (i = 0; i < x.length; i++) { txt = txt + x.options[i].text + "<br>"; }
The result of x will be:
Apple Orange Pineapple Banana
Example 8
Select an option from the dropdown list and output the text of the selected option in the element with id="demo":
var x = document.getElementById("mySelect"); var i = x.selectedIndex; document.getElementById("demo").innerHTML = x.options[i].text;
The result of x will be:
Banana
Example 9
Modify the options in the dropdown list based on the selected option in another dropdown list:
var carsAndModels = {}; carsAndModels['VO'] = ['V70', 'XC60', 'XC90']; carsAndModels['VW'] = ['Golf', 'Polo', 'Scirocco', 'Touareg']; carsAndModels['BMW'] = ['M6', 'X5', 'Z3']; function ChangeCarList() { var carList = document.getElementById("car"); var modelList = document.getElementById("carmodel"); var selCar = carList.options[carList.selectedIndex].value; while (modelList.options.length) { modelList.remove(0); } var cars = carsAndModels[selCar]; if (cars) { var i; for (i = 0; i < cars.length; i++) { var car = new Option(cars[i], i); modelList.options.add(car); } } }