Javascript DOM HTML Datalist Object get

Introduction

The Datalist object represents an HTML <datalist> element.

The <input> element's list attribute can bind together with the datalist element.

We can access a <datalist> element via document.getElementById():

var x = document.getElementById("myDatalist");

Click the button to get the number of option elements in the datalist.

View 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>/*from  ww  w.ja  v  a  2s. co  m*/
<button onclick="myFunction()">Test</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>



PreviousNext

Related