Find out how many options there are in a specific <datalist> element:
The result of x will be:
More "Try it Yourself" examples below.
The options collection returns a collection of all the options in a <datalist> element.
Note: The elements in the collection are sorted as they appear in the source code.
Collection | |||||
---|---|---|---|---|---|
options | Yes | Yes | Yes | Yes | Yes |
Note: The <datalist> element is not supported in Internet Explorer 9 (and earlier versions), or in Safari.
Property | Description |
---|---|
length | Returns the number of <option> elements in the collection. Note: This property is read-only |
Method | Description |
---|---|
[index] | Returns the <option> element from the collection with the specified index
(starts at 0). Note: Returns null if the index number is out of range |
item(index) | Returns the <option> element from the collection with the specified index
(starts at 0). Note: Returns null if the index number is out of range |
namedItem(id) | Returns the <option> element from the collection with the specified id. Note: Returns null if the id does not exist |
DOM Version: | Core Level 2 Document Object |
---|---|
Return Value: | An HTMLCollection Object, representing all <option> elements in the <datalist> element. The elements in the collection are sorted as they appear in the source code |
[index]
Get the value of the first option (index 0) in a datalist:
The result of x will be:
item(index)
Get the value of the first option (index 0) in a datalist:
The result of x will be:
namedItem(name_or_id)
Get the value of the option with id="google" in a datalist:
The result of x will be:
Loop through all options in a datalist, and output the option values:
The result of txt will be: