2

I would like to set a few textareas up so that when a user clicks on a field it will automatically select all and issue a control-c or command-c (depending on the os) in order to copy the text to the clipboard.

nickf
  • 537,072
  • 198
  • 649
  • 721
ylluminate
  • 12,102
  • 17
  • 78
  • 152
  • Have you tried _anything_ yourself? We won't give you a complete written solution. – Bojangles Mar 21 '12 at 22:38
  • 1
    @Tats_innit: That has nothing to do with automatically triggering a copy operation. Also, the `ctrlKey: true;` at the top of the handler is a [label](http://es5.github.com/#x12.12) which is unused; it may as well not be there. – T.J. Crowder Mar 21 '12 at 22:55

4 Answers4

3

I'm relatively sure that this isn't possible. Other places which do this (eg: Github) use a Flash embed which does actually have the ability to do that.

This is what Github uses: https://github.com/mojombo/clippy

nickf
  • 537,072
  • 198
  • 649
  • 721
  • Seems like this fellow has it pretty much sorted except not quite seeing some of the connections for my scenario with multiple textarea fields & it would need an OS test: http://stackoverflow.com/a/9481957/320681 – ylluminate Mar 21 '12 at 22:45
  • @ylluminate: That doesn't automatically trigger the copy. The user has to press Ctrl+C. All that that does is ensure that the text is selected when they do. There's no pure JavaScript, cross-browser way to do this, you have to use Flash or an applet or similar. (Once there *is* a clipboard API in JavaScript in browsers, expect your users to have to grant you permission. Really, they should have to with Flash.) – T.J. Crowder Mar 21 '12 at 22:47
  • @T.J.Crowder It appears to initiate a control-c as I can see. I would need to add an OS test to determine whether cmd-c or ctrl-c. – ylluminate Mar 21 '12 at 22:49
  • 1
    @ylluminate: With respect, you're misreading the code. All that that code does is hook the `keydown` event and watch to see if Ctrl+C is pressed. If it is, it fires the callback. The example callback fills a textarea with text, selects it, and gives it focus so that when the browser's default behavior occurs, that's the text it copies. The user is still the one doing the Ctrl+C. – T.J. Crowder Mar 21 '12 at 22:50
1

Check out zClip, it is a jQuery plugin that allows you to do this:

http://www.steamdev.com/zclip/

It does use Flash to copy to the clipboard, but that's to avoid the browser restrictions preventing this from being done only in javascript.

GoldenNewby
  • 4,382
  • 8
  • 33
  • 44
1

I'm not sure there is a cross browser javascript solution, but you will find some tips here: How to copy text to the client's clipboard using jQuery? as mentioned a lot of people seem to use flash.

Community
  • 1
  • 1
benedict_w
  • 3,543
  • 1
  • 32
  • 49
0

This is usually impossible as it greatly depends on the browser and it's settings. It is off by default for security reasons. The only way to implement this is to use zeroclipboard

Url of the library: zeroclipboard and example

Arman P.
  • 4,314
  • 2
  • 29
  • 47