Datalist options Collection - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Datalist

Description

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

Properties

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

Methods

MethodDescription
[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>
<html>
<body>

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

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

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

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

</body>
</html>

Related Tutorials