PDA

View Full Version : how to have multiple "copy to clipboard"-buttons?


Ardor
10-30-2002, 02:23 PM
Hi,

I found a script on this page http://www.htmlgoodies.com/beyond/clipboard.html and would like some help in defining the variables.

If the script for one "copy to clipboard"-button goes like this:

<SCRIPT LANGUAGE="JavaScript">

function ClipBoard()
{
holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}

</SCRIPT>

and the command for copying 1 piece of text goes like this:

<SPAN ID="copytext" STYLE="height:150;width:162;background-color:pink">
This 1st text will be copied onto the clipboard when you click the button next to the text.
</SPAN>

<TEXTAREA ID="holdtext" STYLE="display:none;">
</TEXTAREA>
<BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>

How should I define the variables if I also want to have:

<SPAN ID="copytext" STYLE="height:150;width:162;background-color:pink">
This 2nd text will be copied onto the clipboard when you click the button next to the text.
</SPAN>

<TEXTAREA ID="holdtext" STYLE="display:none;">
</TEXTAREA>
<BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>

on the same page???

Thanx for all your help!

Note: Please bear in mind that this is the first time I've ever used Javascript!

Mr J
10-30-2002, 02:43 PM
Will This do you




<script>
function Copy(n){
copyme=n
textRange = document.body.createTextRange();
textRange.moveToElementText(copyme);
textRange.execCommand("Copy");
alert(n+ " has been copied to your clipboard")
}
</script>

<P><input type=BUTTON onclick=Copy(Copyit) value="Copy To Clipboard">
<P><div id=Copyit>First copied text</DIV>

<P><input type=BUTTON onclick=Copy(Copyit2) value="Copy To Clipboard 2">
<P><div id=Copyit2>Second text to be copied</DIV>

<P><input type=BUTTON onclick=Copy(Copyit3) value="Copy To Clipboard 3">
<P><div id=Copyit3>And finally the third</DIV>

Ardor
10-30-2002, 06:51 PM
Thank You!!

Works like a charm :)

Mr J
10-30-2002, 07:48 PM
Glad to be of help just amend, or take out the alert I left in while testing it