Add <Option> to <select> - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Option

Description

Add <Option> to <select>

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//www.  j a va2s. co m
var values = ["First", "Second", "Third"];
var oDDL = document.getElementById("my");
for (var i = 0; i < values.length; i++) {
    var option = new Option();
    option.value = option.text = values[i];
    oDDL.appendChild(option);
}
    }

      </script> 
   </head> 
   <body> 
      <select id="my"></select>  
   </body>
</html>

Related Tutorials