YouTip LogoYouTip

Coll Datalist Options

# Datalist options Collection [![Image 8: Datalist Object Reference Manual](#) Datalist Object](#) ## 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.value + "
"; } The output of _txt_ is: Internet Explorer Firefox Chrome Opera Safari [Try it Β»](#) * * * ## Definition and Usage The options collection returns a collection of all option elements in a datalist. **Note:** The elements in the collection are sorted in the order they appear in the code. * * * ## Browser Support ![Image 9: Internet Explorer](#)![Image 10: Firefox](#)![Image 11: Opera](#)![Image 12: Google Chrome](#)![Image 13: Safari](#) The options collection is supported by all major browsers. * * * ## Syntax _datalistObject_.options ## Properties | Property | Description | | --- | --- | | length | Returns the number of option elements in the collection | ## Methods | Method | Description | | --- | --- | | | An integer specifying the element to retrieve (starts at 0) | | item(_index_) | Returns the element at the specified index in the collection (starts at 0) | | namedItem(_name_or_id_) | Returns the element from the collection with the specified name (name or id attribute) | * * * ## More Examples ## Example Display the number of datalist elements: var x = document.getElementById("browsers").options.length; The output of _x_ is: 5 [Try it Β»](#) ## Example Return the value of the first option (index 0) in the datalist: var x = document.getElementById("browsers").options.value; The output of _x_ is: Internet Explorer [Try it Β»](#) ## Example item(_index_) Return the value of the first option (index 0) in the datalist: var x = document.getElementById("browsers").options.item(0).value; The output of _x_ is: Internet Explorer [Try it Β»](#) ## Example namedItem(_name_or_id_) Return the value of the option with id="google" in the datalist: var x = document.getElementById("browsers").options.namedItem("google").value; The output of _x_ is: Chrome [Try it Β»](
← Ruby StringDom Obj Datalist β†’