Javascript Form How to - Add option to select control








Question

We would like to know how to add option to select control.

Answer


<html>
<body>
    <script language="JavaScript">
    function add(myForm) {<!--   w ww.j  a  va2 s . c om-->
       var option0 = new Option("A", "person1");
       var option1 = new Option("B", "person2");
       var option2 = new Option("C", "person3");
       var option3 = new Option("D", "person4");
       for (var i=0; i < 4; i++) {
          eval("myForm.people.options[i]=option" + i)
       }
    }
    </script>
    <form>
    <select name="people" multiple></select><P>
    <input type="button" value="Add People" onClick="add(this.form)">
    </form>
</body>
</html>

The code above is rendered as follows: