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
| SitePoint Sponsor |
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


Programming Group Advisor
Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
Car is to Carpet as Java is to JavaScript

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.
Code: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; }
Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">
Bookmarks