This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes HTML, CSS and JavaScript and the fly likes Copy-to-clipboard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Copy-to-clipboard " Watch "Copy-to-clipboard " New topic
Author

Copy-to-clipboard

Zoram Paul
Ranch Hand

Joined: Jul 10, 2007
Posts: 59
Hello,

I have a textbox and a button.
When I click on the button I want to copy the textbox value so that I can paste it to anywhere.

Can anyone tell me how to do that using javascript?

Thanks


ZORAM<br />"If it's true that we are here to help others,<br />then what exactly are the others here for?"
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
IE

FF + here


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Zoram Paul
Ranch Hand

Joined: Jul 10, 2007
Posts: 59
Although Valentin Crettaz, your code supports only Internet Explorer and Not FireFox........

Here is a better solution and its working for any browser:

<script>
function cSn(sn, el)
{
if (window.clipboardData) {
window.clipboardData.setData("Text",sn);
} else {
var flashcopier = 'flashcopier';
if(!document.getElementById(flashcopier)) {
var divholder = document.createElement('div');
divholder.id = flashcopier;
document.body.appendChild(divholder);
}
document.getElementById(flashcopier).innerHTML = '';
var divinfo = '<embed src="snippetty.swf" FlashVars="clipboard='+escape(sn)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
document.getElementById(flashcopier).innerHTML = divinfo;
}
}

</script>





[ UD: removed gratuitous graemlins ]
[ September 28, 2007: Message edited by: Ulf Dittmer ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The links contain both a solution for IE and FF.

Your code, however, won't work if Flash is not available or cannot be installed for security reasons (i.e., in highly secure intranets!)
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The JavaScript solutions normally do not work now with the newer versions of browsers because the copy,paste can be diabled. This question has come up a lot lately on other forums.

So if the option is not enabled in the browser and flash is disabled, people are stuck using their own fingers to right click or ctrl-c.

Eric
[ September 28, 2007: Message edited by: Eric Pascarello ]
 
Don't get me started about those stupid light bulbs.
 
subject: Copy-to-clipboard