Skip to content

Instantly share code, notes, and snippets.

@LucasArruda
Created November 26, 2020 00:15
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 LucasArruda/a054e7d970950f892974b981fcb4c18d to your computer and use it in GitHub Desktop.
Save LucasArruda/a054e7d970950f892974b981fcb4c18d to your computer and use it in GitHub Desktop.
Copies a supplied text to the clipboard. Useful for react components that loop and can't depends on ids.
// Copies 'text' to clipboard. Doesn't depends on id's, targets. Just text.
const copyToClipboard = (text) => {
const el = document.createElement('input');
document.body.appendChild(el);
el.value = text;
el.select();
el.setSelectionRange(0, 99999); /*For mobile devices*/
document.execCommand("copy");
document.body.removeChild(el);
};
// call it with
copyToClipboard('text to copy');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment