Skip to content

Instantly share code, notes, and snippets.

@metodribic
Created January 9, 2019 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metodribic/75647d5083f0ff1de0eee7c7c1016be7 to your computer and use it in GitHub Desktop.
Save metodribic/75647d5083f0ff1de0eee7c7c1016be7 to your computer and use it in GitHub Desktop.
Copy text to clipboard with pure JavaScript
function copy(text) {
const textarea = document.createElement("textarea");
textarea.style.opacity = '0';
textarea.style.position = 'fixed';
textarea.textContent = text;
const body = document.getElementsByTagName('body')[0];
body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
body.removeChild(textarea);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment