title: Copy text from a hidden control using JavaScript
body: <p>I want to be able to place text in a textarea, and copy it to the clipboard, without displaying the textarea.</p>
  
  <p>To copy the text to the clipboard, I create a button with</p>
  
  <pre><code>onclick = "document.getElementById('txtData').select(); document.execCommand('copy');"
  </code></pre>
  
  <p>and this works fine.</p>
  
  <p>However, if I try to hide the textarea using either</p>
  
  <pre><code>strStyle = 'display:none;'
  
  strStyle = 'visibility:hidden;'
  </code></pre>
  
  <p>as per</p>
  
  <p><a href="https://stackoverflow.com/questions/2362498/if-i-want-my-textarea-to-be-hidden-how-do-i-do-it">If I want my textarea to be hidden, how do I do it?</a></p>
  
  <p>then the copy no longer works. The textarea is hidden in both cases, but I have checked the source HTML and the text I want is still there - it's not like hiding the textarea means its contents aren't actually available.</p>
  
  <p>If a full example is appropriate, here's one I took from <a href="http://www.jstips.co/en/javascript/copy-to-clipboard/" rel="nofollow noreferrer">http://www.jstips.co/en/javascript/copy-to-clipboard/</a></p>
  
  <pre><code>&lt;head&gt;
  &lt;script type='text/javascript'&gt;
  function c2cb () {
  document.getElementById("txtInvoice").select();
  document.execCommand('copy');
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body &gt;&lt;form id="frmAdminConsole" name="frmAdminConsole" METHOD="POST"&gt;
  
  &lt;textarea id='txtInvoice' cols='80'  style = 'visibility:hidden;'&gt;
  
  
  
          46
  
  
  JOHN SMITH
  GAEL SCOIL NA BFHAL
  34 A IVEAGH CRESENT
  BELFAST
  BT12 6AW
  
  
  
  Bubble Ball Football [2017-02-03 09:00]     20  190.00
  Nerf Wars [2017-02-05 10:00]    14  190.00
  TeamTrek [2017-02-06 12:00]     20  0.00&lt;/textarea&gt;
  
  &lt;input type="button" value="Copy!" onclick="c2cb()"&gt;
  </code></pre>
  
  <p>This doesn't work until I remove " style = 'visibility:hidden;'" .</p>
  
  <p>I had what I thought was a D'oh! moment and said "I should use a hidden control", but it doesn't work either. </p>
  
  <p>I would settle for a way to hide the textarea by stealth (make it the same colour as the background or something).</p>