Skip to content

Instantly share code, notes, and snippets.

@kamilklkn
Created January 26, 2021 22:56
Show Gist options
  • Save kamilklkn/ca93ba972f62f74a50d68e88a542f326 to your computer and use it in GitHub Desktop.
Save kamilklkn/ca93ba972f62f74a50d68e88a542f326 to your computer and use it in GitHub Desktop.
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt(0)
: false;
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment