0

A lot of websites have a button which copies text at the click of a button. I found one code here I was trying to make this code work, without any success. What is it missing?

HTML:

<SPAN ID="copytext" 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>

Javascript

<SCRIPT LANGUAGE="JavaScript">
function ClipBoard() 
{
holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}

Aman
  • 387
  • 8
  • 33
  • In what browser are you trying this? The `execCommand` currently only works in Firefox as it is an editor's draft. You can see this [here](https://developer.mozilla.org/en-US/docs/Web/API/document.execCommandBrowser_Compatibility) – Jerodev Oct 09 '14 at 09:46
  • You should see http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript and https://github.com/zeroclipboard/zeroclipboard – laruiss Oct 09 '14 at 09:50
  • @Jerodev I want it work work mainly in Chrome – Aman Oct 09 '14 at 09:57
  • why is Click to copy considered unsafe? – Aman Oct 09 '14 at 10:03
  • @AlexR. Then you will have to search for other alternatives. – Jerodev Oct 09 '14 at 11:01

0 Answers0