Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create "Copy" button?

Ok say you have this:

<input id="test" value="" />

Value of that input is dynamicly generated.

Now a user can double click on that input to select value, ctrl+c to copy.

I also found that with javascript you can select value of input with "click".

But when it comes to creating a button that when clicked will copy value of that input into OS's clipboard - i have found only solutions that works with Flash.

Now i want to know how can i create that button with javascript? Ofc if it is possible... :)

like image 284
Gavrisimo Avatar asked Sep 05 '09 14:09

Gavrisimo


People also ask

How do I make a click to copy?

Whether it is a sample of code or it is the User's own information, we can create a copy button to copy data to the clipboard using the navigator. clipboard. writeText() function. This function writes the data fed to it as a parameter into the clipboard.

Where is the copy button?

To copy and paste in Windows 10 using keyboard shortcuts, first select the item you want to copy. Then press Ctrl + C on your keyboard to copy.

What is copy button?

Select the text you want to copy and press Ctrl+C. Place your cursor where you want to paste the copied text and press Ctrl+V.


1 Answers

Considering the implications (especially security concerns), copying to/from clipboard is not something that is quite allowed / easy.

Those questions / answsers might help you, about that :

  • How to Copy to Clipboard in Javascript
  • Is it possible to read the clipboard in firefox, safari and chrome using javascript?

In each case, it seems that the solution is to use some little .swf file -- which means working with flash (maybe some old version of flash, actually)...

For instance, you could take a look at the Zero Clipboard library (yeap, flash...) :

The Zero Clipboard library provides an easy way to copy text to the clipboard using an invisible Adobe Flash movie, and a JavaScript interface. The "Zero" signifies that the library is invisible and the user interface is left entirely up to you.

This library is fully compatible with Flash Player 10, which requires that the clipboard copy operation be initiated by a user click event inside the Flash movie. This is achieved by automatically floating the invisible movie on top of a DOM element of your choice.

In pure Javascript, it seems to be possible in Internet Explorer, but not in other browsers...

like image 138
Pascal MARTIN Avatar answered Oct 06 '22 15:10

Pascal MARTIN