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".

function copycodeblock(){
	document.querySelector('#copycode').select();
	document.execCommand('copy');
}

Example:

Add the function above to the html page. Then add the html below.  The "place the Copy to Clipboard" button into your code to activate the copy function.

HTML:

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

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

 

Copyright © Lage.us Website Development | Disclaimer | Privacy Policy | Terms of Use