Forum Moderators: open

Message Too Old, No Replies

javascript for "copy highlighted text"? (not IE only)

         

hommealone

4:46 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Does anyone know of a javascript that would copy (already) highlighted text to your clipboard? Maybe by pushing a form button that will copy text that's been highlighted in a textarea? I've searched, and only found one or two which work only with IE.

Anyone here know of one that has better cross-browser results?

SpaceFrog

4:48 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



firefox has this feature already included

hommealone

5:26 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Hi SpaceFrog,

How so? Do you mean with a right click option? I know that this isn't very important because it is easily accomplished by right click options, but in a particular application that I'm working on, it would be nice to have a button that could be clicked to "copy highlighted text to clipboard".

BTW, I found your script in a nearby post for copying the contents of an element (a div) onto the clipboard, and I'm playing around with that one (although it's apparently just for IE). Thanks.

SpaceFrog

7:59 am on Feb 24, 2005 (gmt 0)

10+ Year Member



yes with a right click and you can even add an extension for a copy as plain text ...

hommealone

3:48 pm on Feb 24, 2005 (gmt 0)

10+ Year Member



SpaceFrog,

Thanks, that's what I thought you meant. What I'm looking for is a javascript to do that at the click of a button, rather than as a right click option. Your script for IE worked well when I adapted it to work with a form button, and I may end up using it. Here's what I did with it:

function sendToClipboard(s)
{
if( window.clipboardData && clipboardData.setData )
{
clipboardData.setData("Text", s);
}
else
{
alert("Internet Explorer required");
}
}

<body>
<input type="button" value="Copy to Clipboard" onclick="sendToClipboard(document.getElementById('textareaIDgoes here').innerHTML)" />
</body>


But still I was hoping that someone knew a script that worked on other browsers as well.

Anyone?