Create Datalist Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Datalist

Introduction

You can create a <datalist> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm">
</form>//from  ww w  . ja v  a 2 s.  c om

<button onclick="myFunction()">create an INPUT field, a DATALIST element and an OPTION element</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>

Related Tutorials