Asked 7 years ago
30 Dec 2016
Views 2515
web-api

web-api posted

how to Copy to clipboard by JavaScript

how to Copy to clipboard by JavaScript


<textarea id="textarea">Copy to clipboard</textarea>
<input type="button" value="copy to clipboard" onclick="CopyToClipBoard()">
<script type="text/javascript">
function CopyToClipBoard(){
var text=$('#textarea').val();
//copytoclipboard(text);
}
</script>


after getting value of textarea or input box . how to copy it to clipboard so one can paste at where he want.
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

ZeroClipboard.js can be used for copy text to clipboard but if you dont like flash and dont want to use flash in your web application .you need to avoid this .because ZeroClipboard.js use an invisible Adobe Flash movie and a JavaScript interface to copy text to clip board

code is

    <script src="ZeroClipboard.js"></script>

   <button id="copy-button" data-clipboard-text="Copy Me!" title="Click to copy me.">Copy to Clipboard</button>
<script type="text/javascript">
var client = new ZeroClipboard( document.getElementById("copy-button") );

client.on( "ready", function( readyEvent ) {
  // alert( "ZeroClipboard SWF is ready!" );

  client.on( "aftercopy", function( event ) {
    // `this` === `client`
    // `event.target` === the element that was clicked
    event.target.style.display = "none";
    alert("Copied text to clipboard: " + event.data["text/plain"] );
  } );
} );
</script>


hope it work
ravi

ravi
answered Nov 30 '-1 00:00

clipboard.js will help you on coping text to clipboard without flash or complex framework



<script src="https://cdn.jsdelivr.net/clipboard.js/1.5.16/clipboard.min.js"></script>
<textarea id="textarea">Copy to clipboard</textarea>
<button class="btn" data-clipboard-action="copy" data-clipboard-target="#textarea" >Copy
 </button>

Nilesh

Nilesh
answered Nov 30 '-1 00:00

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
});

Post Answer