0

I am working on a website project. For that I need to copy the text from the textbox to the clipboard so that it can be pasted into a notepad or somewhere else. I am using Visual studio 2008 with c#.

I have written this code but it's not working:

<script language="javascript" type="text/javascript">
    function ClipBoard()
    {
        TextBox1.innerText = Button1.innerText;
        Copied = TextBox1.createTextRange();
        Copied.execCommand("RemoveFormat");
        Copied.execCommand("Copy");
    }
</script>
<asp:TextBox ID="TextBox1" runat="server">Click on the button to copy the this text</asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="Copy Text" onclick="ClipBoard();" />

In this code 2 errors are occuring on the last line ")expected" and "invalid expresion term ')'"

Please help me if someone knows the solution for that.

Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
swati sharma
  • 21
  • 1
  • 3
  • 5
  • 2
    possible duplicate of [How to copy to the clipboard in JavaScript?](http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript) – Jon Mar 20 '12 at 08:46
  • Check the answer to the question linked by @Jon. Adding a little flash object to actually copy the text to clipboard seems the easiest way. – CodingBarfield Mar 20 '12 at 09:54
  • You use OnClientClick() to execute JavaScript. OnClick() is for server side methods. – Dominic Zukiewicz Mar 20 '12 at 12:01

1 Answers1

0

Check the answer to the question linked by @Jon. Adding a little flash object to actually copy the text to clipboard seems the easiest way.

Something like this should do the trick

function copyIntoClipboard(text) {

var flashId = 'flashId-HKxmj5';

/* Replace this with your clipboard.swf location */
var clipboardSWF = 'http://appengine.bravo9.com/copy-into-clipboard/clipboard.swf';

if(!document.getElementById(flashId)) {
    var div = document.createElement('div');
    div.id = flashId;
    document.body.appendChild(div);
}
document.getElementById(flashId).innerHTML = '';
var content = '<embed src="' + 
    clipboardSWF +
    '" FlashVars="clipboard=' + encodeURIComponent(text) +
    '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
document.getElementById(flashId).innerHTML = content;
}

Humbly copied from Copy / Put text on the clipboard with FireFox, Safari and Chrome

Community
  • 1
  • 1
CodingBarfield
  • 3,392
  • 2
  • 27
  • 54