Copy Text to Clipboard

Copies text to clipboard. It must copy from a textarea.

There are three things to add to the page to make this work.

1. Add the function copycodeblock()
2. Create a textarea to hold the text to be copied.
3. Create a button or link that activates the function.

If a textarea is not wanted in the design of the page, the text can be copied from a div or span using innerHTML into a hidden textarea with width and height of 0.

The line document.querySelector('#copycode').select(); causes the code section  in the textarea to be selected. The textarea needs an id="copycode".


Add the function above to the html page. Then add the html below.  The "Copy to Clipboard" button above is a working example of this function.


<textarea id="copycode">
 -- Some text to copy --

<button onclick="copycodeblock()">Copy to Clipboard</button>