Created
December 7, 2016 06:53
-
-
Save kisin/2934fed411972fecca01405ab0672191 to your computer and use it in GitHub Desktop.
copy to clipboard
This file contains 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
(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