Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi All,

I have a hidden field as below.

<input id="hdnKopierModtagere" class="hdnKopierModtagere" type="hidden" runat="server" />

The value of this field is set in the code-behind.

On clicking a button, i have to copy the text from the hidden control to the clipboard.

I have searched in google a lot, but most of them recommend to use Flash or ZeroClipboard. But I can't use this.

I need a pure javascript to copy the text to the clipboard and it should be supported in following browsers (IE, Mozilla and Chrome). Kindly advice me know how to proceed.

Thanks in advance.
Posted
Comments
Richard MacCutchan 3-Aug-15 11:42am    
You cannot use the clipboard in Javascript. It belongs to the client.
nimalan sivakumar 3-Aug-15 11:45am    
Thanks for your reply. My question is I want to copy the text in hidden control to the Clipboard.
Richard MacCutchan 3-Aug-15 11:57am    
You already said that, and I told you why you cannot. The clipboard belongs to the client, and if your web page starts dumping things in it you may destroy hours of their work. There are other ways to save information on the client: session variables, cookies etc. You need to follow the protocol, not try to break it.
nimalan sivakumar 3-Aug-15 12:09pm    
My purpose is, after the clicking the button, the user can paste the copied text in notepad or MS word. Therefore session variables and cookies doesnt serve my purpose
Richard MacCutchan 3-Aug-15 12:13pm    
Well you still can't do it.

1 solution

Look at any code sample in any CodeProject article or answer. You will see "Copy code" anchors over the right top corner of each code ("<pre>") element. Click at this anchor and see what it does. I suggest you do exactly the same thing. It helps the reader to easily copy the text in the clipboard in a non-intrusive manner, in contrast to what you are trying to do.

—SA
 
Share this answer
 
Comments
nimalan sivakumar 4-Aug-15 2:29am    
Thanks for your reply. In the sample which you have mentioned, the user has to do the copy using Ctrl+C.
But for my scenario, the clciking of button should automatically copy the text.
Sergey Alexandrovich Kryukov 4-Aug-15 2:35am    
No, it shouldn't. And this is not a "reply", this is a solution.
—SA
nimalan sivakumar 4-Aug-15 3:03am    
In the solution you have mentioned, on the click of "Copy code", a new window is opened which displays the text. From there the user has to manually copy the code using "Ctrl+C"
Sergey Alexandrovich Kryukov 4-Aug-15 3:25am    
I mean the same thing Richard MacCutchan told you in his comment to the question. If you could do it, it could be too intrusive, because the user could have saved in clipboard something else, which could be lost. Reasonable users do want to press on Ctrl+C Ctrl+Insert (same thing), or menu item...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900