Skip to content

Instantly share code, notes, and snippets.

@justforuse
Created December 3, 2020 06:45
Show Gist options
  • Save justforuse/9940f6328235adbc43c310ec9993666a to your computer and use it in GitHub Desktop.
Save justforuse/9940f6328235adbc43c310ec9993666a to your computer and use it in GitHub Desktop.
copy string to your clipboard by javascript
function copyToClipboard(str) {
if (!str) {
return;
}
// input element cannot keep '\n', so you can turn to textarea
const inputEl = document.createElement('textarea');
inputEl.value = str;
inputEl.style.cssText = 'position: absolute; top: -9999px; left: -9999px;';
document.body.appendChild(inputEl);
inputEl.select();
inputEl.setSelectionRange(0, 99999);
document.execCommand('copy');
document.body.removeChild(inputEl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment