Datalist options Collection - Loop through all options in a datalist, and output the option values: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Datalist

Description

Datalist options Collection - Loop through all options in a datalist, and output the option values:

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>//  w w w.jav  a2s. c  o m

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

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

<script>
function myFunction() {
    var x = document.getElementById("browsers");
    var txt = "";
    var i;
    for (i = 0; i < x.options.length; i++) {
        txt = txt + x.options[i].value + "<br>";
    }

    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials