Simple JavaScript Copy Feature

How can I make a simple copy and paste for text in JavaScript? I would like to achieve this when I select some text in textarea , then I can click on the button to copy it, then I can go to another page with the right mouse button in another textarea and select paste.

+5
source share
4 answers

Take a look at this library: https://github.com/zeroclipboard/zeroclipboard

You cannot access the clipboard in JavaScript, which means that flash is more or less the only option.

+2
source

Use this

 function Copy() { if(window.clipboardData) { window.clipboardData.clearData(); window.clipboardData.setData("Text", document.getElementById('txtacpy').value); } } function paste() { if(window.clipboardData) { document.getElementById('txtapaste').value = window.clipboardData.getData("Text"); } } 
  <a href="javascript:Copy();">Copy</a> <br /> <input type="text" name="txtacpy" id ="txtacpy"/> <br /> <a href="javascript:paste();">Paste</a> <br /> <input type="text" name="txtapaste" id="txtapaste"/> 

Its simple copy and paste function. It works well with IE.

I hope this helps you

+1
source

Assuming you want to receive custom keyboard actions, you probably want to use keyboard shortcuts: https://github.com/jeresig/jquery.hotkeys

0
source

I think the easiest way (and work in all browsers) is to watch the keys pressed by the user, and if he press CTRL + C, save some data that you want to copy to some variable.

I mean something like this:

  var myClipboardVariable; document.onkeyup = function(e){ if ((e.key == 'c') && e.ctrlKey){ // save data (you want to copy) into variable myClipboardVariable = ....//some data } if ((e.key == 'v') && e.ctrlKey){ // paste saved data .... paste your data from variable myClipboardVariable } } 
0
source

Source: https://habr.com/ru/post/1347072/


All Articles