Skip to content

Instantly share code, notes, and snippets.

@aslafy-z
Forked from lgarron/copyToClipboard.html
Created July 21, 2016 13:57
Show Gist options
  • Save aslafy-z/9911ac4dc66f3eaf13009a7256ac9c79 to your computer and use it in GitHub Desktop.
Save aslafy-z/9911ac4dc66f3eaf13009a7256ac9c79 to your computer and use it in GitHub Desktop.
<script>
// A simple function to copy a string to clipboard. See https://github.com/lgarron/clipboard.js for a full solution.
var copyToClipboard = (function() {
var _dataString = null;
document.addEventListener("copy", function(e){
if (_dataString !== null) {
try {
e.clipboardData.setData("text/plain", _dataString);
e.preventDefault();
} finally {
_dataString = null;
}
}
});
return function(data) {
_dataString = data;
document.execCommand("copy");
};
})();
</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