0

Possible Duplicate:
How to copy to the clipboard in JavaScript?

I have an html markup thats echoed and is wrapped in a textarea and its also a read only like so:

<textarea rows="4" cols="70" readonly="readonly">text with markup goes here.</textarea>

how do I do it that when a user clicked on the read-only text area contents, it will be automatically selected and copied to the clipboard and with a notification that its already copied?

Community
  • 1
  • 1
anagnam
  • 351
  • 3
  • 22

2 Answers2

2

PHP is server-side so you won't be able to do anything to interact with the client. In terms of client-side then javascript can do something. The bad news is that Firefox doesn't allow this any more as far as I recall (I remember seeing some debate around it regarding TinyMCE's clipboard functionality) so the odds are you'll need something mixing flash and js to get a fully cross-browser solution.

Might I thus suggest http://code.google.com/p/zeroclipboard/ as a possibility?

nospamthanks
  • 112
  • 2
  • 13
0

Why do you want PHP for that,try this:

function copy(inElement) {
 if (inElement.createTextRange) {
  var range = inElement.createTextRange();
 if (range)
   range.execCommand('Copy');
} else {
  var flashcopier = 'flashcopier';
  if(!document.getElementById(flashcopier)) {
  var divholder = document.createElement('div');
   divholder.id = flashcopier;
  document.body.appendChild(divholder);
}
   document.getElementById(flashcopier).innerHTML = '';
   var divinfo = '<embed src="_clipboard.swf"        FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0"      type="application/x-shockwave-flash"></embed>';
document.getElementById(flashcopier).innerHTML = divinfo;
}
 }

<form name="formtocopy" action="" >
<textarea name="texttocopy" disabled="disabled">
A whole bunch of text here that will be copied.
</textarea>
<br>
<a href="javascript:copy(document.formtocopy.texttocopy);">Copy the Text!</a>
</form>
Milind Anantwar
  • 77,788
  • 22
  • 86
  • 114