Create an Option Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Option

Introduction

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<select id="mySelect">
</select>/*from w  w w  . ja  v a2 s . co  m*/

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

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

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

</body>
</html>

Related Tutorials