views:

547

answers:

4

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... :)

+3  A: 

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 :

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...

Pascal MARTIN
So it would seem... <_< t-up for answer, but i will wait some more before marking this as accepted answer... :)
GaVrA
@GaVrA : I'm curious about other answers too, actually ;-) ;; As a user who doesn't allow flash by default (I use the flashblock plugin for firefox), I don't like flash-based solutions ^^
Pascal MARTIN
@Pascal MARTIN i dont want to be so harsh, but i will say that i hate flash. So if it can be done without using it - kudos! :) Cant wait for HTML5...
GaVrA
@GaVrA : without going to something as strong as "hate", I quite understand what you mean ^^
Pascal MARTIN
A: 
<input type="button" 
       value="" 
       onclick="this.createTextRange().execCommand('Paste');"/>
adatapost
the OP is asking about "copy", and not "paste" -- so I guess you need to use "Copy", and not "Paste" in the execCommand line ;; and : does this work in anything else that IE ? (Looks like "IE-only code")
Pascal MARTIN
So when i click button i will copy value of that button? Not what im looking for.
GaVrA
So use **Copy** command.
adatapost
I did and it does not work in firefox. I need this to work in all browsers with javascript, not using some ie specific thingy.
GaVrA
A: 

I found this:

      window.fCopyToClipboard = function(rSource){
         rSource.select()
          if(window.clipboardData){ 
           var r=clipboardData.setData('Text',rSource.value); return 1; 
         }
         else return 0}

And source: http://www.esqsoft.com/javascript-help/how-to-select-html-input-and-copy-to-clipboard.htm

Rin
I forgot to mention, i did use google... Which means i have found that page and guess what? It does not work.
GaVrA
A: 

Can i Create Copy Button in my website for giving visitors HTMl Code? plz its urgent i need to make Html Code for my website..............

Jignesh