-
-
Save crazy4groovy/7247f408dc9b49542c72191c22328947 to your computer and use it in GitHub Desktop.
Copy any text to clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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