How to add Drop-Down list (<select>) programmatically? How to add Drop-Down list (<select>) programmatically? javascript javascript

How to add Drop-Down list (<select>) programmatically?


This will work (pure JS, appending to a div of id myDiv):

Demo: http://jsfiddle.net/4pwvg/

var myParent = document.body;//Create array of options to be addedvar array = ["Volvo","Saab","Mercades","Audi"];//Create and append select listvar selectList = document.createElement("select");selectList.id = "mySelect";myParent.appendChild(selectList);//Create and append the optionsfor (var i = 0; i < array.length; i++) {    var option = document.createElement("option");    option.value = array[i];    option.text = array[i];    selectList.appendChild(option);}