Javascript DOM HTML Datalist Object create

Introduction

We can create a <datalist> element via the document.createElement() method:

Click the button to create an INPUT field, a DATALIST element and an OPTION element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<form id="myForm">
</form>//from   w  w  w.j  a  va2s .  co m

<button onclick="myFunction()">Test</button>
<script>
function myFunction() {
  var x = document.createElement("INPUT");
  x.setAttribute("list", "browsers");
  document.getElementById("myForm").appendChild(x);

  var y = document.createElement("DATALIST");
  y.setAttribute("id", "browsers");
  document.getElementById("myForm").appendChild(y);

  var z = document.createElement("OPTION");
  z.setAttribute("value", "Chrome");
  document.getElementById("browsers").appendChild(z);
}
</script>

</body>
</html>



PreviousNext

Related