Copy to clipboard using javascript

In a current project i am working on, I need to be able to copy some posted content to my clipboard. Well I looked for a solution so I thought I’d show you….


<span id="texttobecopied"  STYLE="height:150;width:162;background-color:pink">
This text will be copied onto the clipboard when you click the button below. Try it!
</SPAN> 
<TEXTAREA ID="holdtext" STYLE="display:none;">
</TEXTAREA>
<BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>

We have a button here and a span that contains text that will be copied. The button is calling “ClipBoard();” . This is a function that we have written in javascript. It is below…


<SCRIPT LANGUAGE="JavaScript">

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

</SCRIPT>

This script reads the inner text of the span, we defined the span on the first line of the function…

 holdtext.innerText = texttobecopied.innerText;

and then holds it in the computers memory. You can assign when clicking an image to copy text or other data by just adding

onclick="ClipBoard();"

To any Link,Image, script ect…

This entry was posted in Coding and tagged , , , , . Bookmark the permalink.

Leave a Reply