Datalist options Collection - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Datalist


The options collection returns a collection of all the options in a <datalist> element.


Property Description
length Returns the number of <option> elements in the collection.


[index] <option> with the specified index (starts at 0).
item(index) <option> with the specified index (starts at 0).
namedItem(id) <option> with the specified id.

Return Value:

An HTMLCollection Object, representing all <option> elements in the <datalist> element.

The following code shows how to Find out how many options there are in a specific <datalist> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

  <input list="browsers" name="browser">
  <datalist id="browsers">
    <option value="A">
    <option value="B">
    <option value="C">
    <option value="D">
    <option value="E">
</form>/*w  w w  .  j av  a  2  s .com*/

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

function myFunction() {
    var x = document.getElementById("browsers").options.length;
    document.getElementById("demo").innerHTML = "Found " + x + " options in the list.";


Related Tutorials