title: Copy output of a JavaScript variable to the clipboard
body: <p>I have no knowledge of JavaScript, but I managed to put this code together using bits and bolts from various Stack&nbsp;Overflow answers. It works OK, and it outputs an array of all selected checkboxes in a document via an alert box.</p>
  
  <pre><code>function getSelectedCheckboxes(chkboxName) {
    var checkbx = [];
    var chkboxes = document.getElementsByName(chkboxName);
    var nr_chkboxes = chkboxes.length;
    for(var i=0; i&lt;nr_chkboxes; i++) {
      if(chkboxes[i].type == 'checkbox' &amp;&amp; chkboxes[i].checked == true) checkbx.push(chkboxes[i].value);
    }
    return checkbx;
  }
  </code></pre>
  
  <p>And to call it I use:</p>
  
  <pre><code>&lt;button id="btn_test" type="button" &gt;Check&lt;/button&gt;
  &lt;script&gt;
      document.getElementById('btn_test').onclick = function() {
          var checkedBoxes = getSelectedCheckboxes("my_id");
          alert(checkedBoxes);
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>Now I would like to modify it so when I click the <code>btn_test</code> button the output array <code>checkbx</code> is copied to the clipboard. I tried adding:</p>
  
  <pre><code>checkbx = document.execCommand("copy");
  </code></pre>
  
  <p>or</p>
  
  <pre><code>checkbx.execCommand("copy");
  </code></pre>
  
  <p>at the end of the function and then calling it like:</p>
  
  <pre><code>&lt;button id="btn_test" type="button" onclick="getSelectedCheckboxes('my_id')"&gt;Check&lt;/button&gt;
  </code></pre>
  
  <p>But it does not work. No data is copied to clipboard.</p>