Skip to content

Instantly share code, notes, and snippets.

@kisin
Created December 7, 2016 06:53
Show Gist options
  • Save kisin/2934fed411972fecca01405ab0672191 to your computer and use it in GitHub Desktop.
Save kisin/2934fed411972fecca01405ab0672191 to your computer and use it in GitHub Desktop.
copy to clipboard
(function() {
'use strict';
// click events
document.body.addEventListener('click', copy, true);
// event handler
function copy(e) {
// find target element
var
t = e.target,
c = t.dataset.copytarget,
inp = (c ? document.querySelector(c) : null);
// is element selectable?
if (inp && inp.select) {
// select text
inp.select();
try {
// copy text
document.execCommand('copy');
inp.blur();
}
catch (err) {
alert('please press Ctrl/Cmd+C to copy');
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment