Javascript DOM HTML Select Object create

Introduction

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

var x = document.createElement("SELECT");

Click the button to create a SELECT and an OPTION element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from  ww w. j ava2s.  c o m
  var x = document.createElement("SELECT");
  x.setAttribute("id", "mySelect");
  document.body.appendChild(x);

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

</body>
</html>



PreviousNext

Related