Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Forked from lgarron/copyToClipboard.html
Last active September 25, 2018 16:36
Show Gist options
  • Save crazy4groovy/7247f408dc9b49542c72191c22328947 to your computer and use it in GitHub Desktop.
Save crazy4groovy/7247f408dc9b49542c72191c22328947 to your computer and use it in GitHub Desktop.
Copy any text to clipboard
<script>
// A simple function to copy a string to clipboard.
// This works in most modern browsers, although you won't be able to tell if the copy command succeeded.
// See https://github.com/lgarron/clipboard-polyfill for a more robust solution.
function copyToClipboard(str) {
function listener(e) {
e.preventDefault();
e.clipboardData.setData("text/plain", str);
}
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
};
</script>
<button onclick="copyToClipboard(this.textContent)">Copy me!</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment