i made something . less code , no library or pure JavaScript to copy clipboard
<textarea id="copyme">Copy to clipboard in pure JavaScript No library or plugin OverHead </textarea>
<input type="button" value="copy to clipboard" onclick="CopyToClipBoard()">
function copyhandler(){
document.execCommand('copy');
}
document.addEventListener('copy', function(e){
e.clipboardData.setData('text/plain',document.getElementById('copyme').value);
e.preventDefault(); // We want our data, not data from any selection, to be written to the clipboard
});
limitation
it only fire when ctrl+c or copy action through browser UI , so i used document.execCommand('copy'); to execute copy command for on click event .
so suppose it you do ctrl+c for any element in same page it copy textarea 's content to clipboard. so it effect on whole page .
we can remove limitation by adding eventlister to particular element . textarea is our case.
following code is only fire when ctr+c or any copy event at textarea .
we change eventlister to textarea
document.getElementById('textarea').addEventListener('copy', function(event){
event.clipboardData.setData('text/plain',document.getElementById('textarea').value);
event.preventDefault(); // We want our data, not data from any selection, to be written to the clipboard
});