dynamically add Select w/Data

SMabs
SMabs used Ask the Experts™
on
I need to dynamically append to a Form new Selects.

anyone got a JS function?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
Use :
document.forms[0].appendChild(document.createElement("select"));

Or :

document.forms["formName"].appendChild(document.createElement("select"));

Or :

var s = document.createElement("select");
s.id = "selectID";
document.forms[0].appendChild(s);
leakim has it right, and to assist I thought I should add how to do it with data


var data = [{value:111,text:"Bob"},{value:222,text:"Peter"}];
var cbo = document.createElement("select");
for (var i in data) { 
    var opt = document.createElement("option"); 
    opt.value = data[i].value; 
    opt.innerHTML = data[i].text; 
    cbo.appendChild(opt); 
} 
document.forms["formName"].appendChild(cbo);

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
Thanks for the points!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial