Onclick Select All and Copy to Clipboard?

Hi I have a textarea with code text in it, and would like to have a link or button which when the user clicks on it, it will select ALL the code and copy it to the clipboard at the same time…

Can anyone help me?

I found this code which seems to work only in IE and it has something strange when I copy it to my HTML…link below…

http://www.sajithmr.com/onclick-select-all-and-copy-to-clipboard-javascript/

Give this a try:


<html>
<title>JavaScript: Select All</title>
<body>
<center>
  <form>
    <textarea name="text_area" rows="10" cols="80">
      blah blah blah blah blah blah blah 
    </textarea>
  <br>
  <input type="button" value="Highlight Text" onClick="javascript:this.form.text_area.focus();this.form.text_area.select();">
  </form>
</center>
</body>
</html>

and to copy the text at the same when you click that button?

The only cross browser way I’ve heard of copying to the system clipboard is to use flash… something like call javascript function to call into movieclip function then inside that call System.setClipboard(string)…

Firefox(Mozilla) doesn’t allow this operation without enhanced privilages (editing prefs files, etc)…

MS is proprietry… window.clipboardData.setData(…) or document.execCommand(‘copy’, false, null)

Opera/Safari… I’m not sure they support anything natively.

So you’re best bet is flash… hope this helps.

Cheers,
WF

When you click the “Highlight Text” button it will select everything in the <textarea>.

@alecrust that will only select the text :slight_smile: the only way I know of copying to the system clipboard using javascript xbrowser is via the flash method.

My fault sorry I thought that was all that was required.

If no one else has an idea how to do it…can you refer to the flash thing to do this?

If you google for “flash javascript clipboard” there’s quite a few solutions people have written, so you should be able to pick one without too must hassle (I think your users will require flash 7+ player)…

I’ve not had to implement this technique yet myself (but there is a project I’m working on that will require this functionality), so all I can do at the mo is point you in a direction :slight_smile:

Hope this helps.

WF