Use list to generate select options

I’d like to extend the issue of using javascript variable in html tag:
My variable is a comma separated list (such as: “a, b, c, d”). The number of elements is changing. I’d like use the list in a SELECT tag.
Any idea how to do it?
Thanks

First you could split the list, then use that list to generate the select options.

You can split the list for example with: ‘a, b,c, d’.split(/, */)
which splits it based on the comma followed by 0 or more spaces

The following function would take the comma separated list of options and return a complete select list containing those values ready to be added to the web page.

createSelect = function(vals) {
/* use strict */
var s, i, v, ln;
s = document.createElement('select');
v = vals.split(/, */);
ln = v.length;
for (i=0; i < ln; i++) {s.options[i] = new Option(v[i], v[i]);}
return s;
}