Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Copy-to-clipboard  RSS feed

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IE

FF + here
 
Zoram Paul
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!)
 
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!