Skip to content

Instantly share code, notes, and snippets.

@mqamarmunir
Forked from lgarron/copyToClipboard.html
Created July 21, 2017 11:14
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 mqamarmunir/aece9a22547814970446a95aaa156ed0 to your computer and use it in GitHub Desktop.
Save mqamarmunir/aece9a22547814970446a95aaa156ed0 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