You can use array.join (','); to create a comma separated list from an array.
Something like this, only better:
var list_to_string = function() { var opts = document.getElementById('list').options; var i = 0, len = opts.length, a = []; for (i; i<len; i++) { a.push(opts[i].value); } document.getElementById('str').value = a.join(','); }
source share