Ti conviene crearti una funzione a cui passare un oggetto descrittore. Così:
codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Documento HTML</title>
<script type="text/javascript">
function addSelect (oDescriptor, sName, oParent) {
var oOpt, oSel = document.createElement("select");
oSel.setAttribute("name", sName);
for (var sLabel in oDescriptor) {
oOpt = document.createElement("option");
oOpt.innerHTML = sLabel;
oOpt.value = oDescriptor[sLabel] === null ? sLabel : oDescriptor[sLabel].toString();
oSel.appendChild(oOpt);
}
oParent.appendChild(oSel);
}
</script>
</head>
<body>
<div id="contenitore"></div>
<span style="text-decoration: underline; color: #0000ff; cursor: pointer;" id="pulsante">Aggiungi select</span>
<script type="text/javascript">
document.getElementById("pulsante").onclick = function () {
addSelect({
"option #1": 14,
"option #2": true,
"pizza": "Ciao mondo!",
"fichi": null
}, "esempio", document.getElementById("contenitore"));
};
</script>
</body>
</html>