Javascript DOM HTML Option Object create

Introduction

We can create an <option> element via the document.createElement() method:

Example

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

Click the button to create an OPTION element.

View in separate window

<!DOCTYPE html>
<html>
<body>

<select id="mySelect">
</select>//from w  ww  .j  a v a2  s.  c  o m

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.createElement("OPTION");
  x.setAttribute("value", "javascript");
  var t = document.createTextNode("Javascript");
  x.appendChild(t);
  document.getElementById("mySelect").appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related