Datalist options collection
Example
Iterate through all options of the datalist control and output the option values:
var x = document.getElementById("mySelect");
var txt = "";
for (var i = 0; i < x.options.length; i++)
{
txt = txt + x.options[i].value + "
";
}
txt The output is:
Internet Explorer
Firefox
Chrome
Opera
Safari
Try it »
Definition and usage
The options collection returns a collection of all options in the datalist.
Note: The elements in the collection are sorted by their position in the code.
Browser support

All major browsers support the options collection.
Syntax
datalistObject
.options
property
| Property | Description |
|---|---|
| length | Returns the number of option elements in the collection |
Method
| Method | Description |
|---|---|
| [index] | An integer specifying the element to retrieve (starting from 0) |
| item(index) | Returns the element at the specified index from the collection (starting from 0) |
| namedItem(name_or_id) | Returns the element from the collection by the specified name (name or id attribute) |
More examples
Example
Display the number of datalist elements:
var x = document.getElementById("browsers").options.length;
x The output is:
5
Try it »
Example
[index]
Returns the value of the first option in the datalist (index 0):
var x = document.getElementById("browsers").options[0].value;
x The output is:
Internet Explorer
Try it »
Example
item(index)
Returns the value of the first option in the datalist (index 0):
var x = document.getElementById("browsers").options.item(0).value;
x The output is:
Internet Explorer
Try it »
Example
namedItem(name_or_id)
Returns the value of the option id="google" in the datalist:
var x = document.getElementById("browsers").options.namedItem("google").value;
x The output is:
Chrome
Try it »
Datalist object