title: Generate a comma delimited string from items in a HTML List Box (multiple select) with Javascript
body: <p>So if you have a html List Box, also called a multiple select, and you want to generate a comma delimited string that lists all the values in that List Box you can you can do that with the following example. The list_to_string() js function is the only important thing here.  You can play with this page at <a href="http://josh.gourneau.com/sandbox/js/list_to_string.html" rel="nofollow noreferrer">http://josh.gourneau.com/sandbox/js/list_to_string.html</a></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;script&gt;
      function list_to_string()
      {
          var list = document.getElementById('list');
          var chkBox = document.getElementById('chk');
          var str = document.getElementById('str');
          textstring = "";
          for(var i = 0; i &lt; list.options.length; ++i){
              comma = ",";
              if (i == (list.options.length)-1){
                  comma = "";
              }
              textstring = textstring + list[i].value + comma;
              str.value = textstring;
          }
  
      }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form&gt;
          &lt;select name="list" id="list" size="3" multiple="multiple"&gt;
              &lt;option value="India"&gt;India&lt;/option&gt;
              &lt;option value="US"&gt;US&lt;/option&gt;
              &lt;option value="Germany"&gt;Germany&lt;/option&gt;
          &lt;/select&gt;
          &lt;input type="text" id="str" name="str" /&gt;
          &lt;br /&gt;&lt;br /&gt;
          &lt;input type="button" id="chk" value="make a string!" name="chk" onclick="list_to_string();"/&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>