Get Datalist Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Datalist

Introduction

The Datalist object represents an HTML <datalist> element.

You can access a <datalist> element by using getElementById():

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form>
  <input list="myDatalist"> 
  <datalist id="myDatalist"> 
    <option value="Internet Explorer">
    <option value="Firefox">
    <option value="Chrome">
    <option value="Opera">
    <option value="Safari">
  </datalist>
</form>/* w w w  .j  a v a  2s  .com*/

<button onclick="myFunction()">get the number of option elements in the datalist</button>

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

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

</body>
</html>

Related Tutorials