Copy To Clipboard
Copy To Clipboard using javascript
by suraj mahajan
HTML
<label for="twitter">Text:</label>
<input type="text" id="twitter" value="suraj mahajan" />
<button data-copytarget="#twitter">copy</button>
JavaScript
(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');
}
}
}
})();