Create a Select Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Select

Introduction

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">create a SELECT and an OPTION element</button>

<script>
function myFunction() {//  ww  w.j  a v a2 s  . c  om
    var x = document.createElement("SELECT");
    x.setAttribute("id", "mySelect");
    document.body.appendChild(x);

    var z = document.createElement("option");
    z.setAttribute("value", "css");
    var t = document.createTextNode("CSS");
    z.appendChild(t);
    document.getElementById("mySelect").appendChild(z);
}
</script>

</body>
</html>

Related Tutorials