Forum Discussion
Copy Text Variable to Clipboard
I need some help.
I need users to be able to click a button to copy a text variable to their clipboard, which can then be pasted in a word doc. This doesn't necessarily have to be done through a text variable, but is the only way I've come to think this will work.
In the past I used a text entry field that when published allows users to copy and paste the text by highlighting and right clicking as you normally would. I've been asked to eliminate that route and only use a one-click copy to clipboard button.
I was able to get this to work using JavaScript, however, this only worked in IE and I desperately need it to work in FireFox. I used the method below from another post to get it to work in IE:
I've sifted through post after post and haven't found anything that will work for this situation. If at all possible, this would be great if this worked with HTML5 as well.
Any help would be greatly appreciated. Thanks!
- BrettSchlagel-eCommunity Member
I'm definitely not super familiar with JS, I've worked with it here and there with success but I'm still having troubles. What you found looks like it would work, I found this earlier and attempted it with no success, but that could purely be from what I wrote, as I am a noob when it comes to JS. Here's the code I tried:
var player = GetPlayer();
var text= player.GetVar("copytext");
function copyToClipboard(text){
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);Is that right? Close? Missing something? When my button (with the execute js trigger when clicked trigger) is clicked it does nothing.
Thanks for the help!
- JacksonHamnerCommunity Member
Try this:
var player = GetPlayer();
var text= player.GetVar("copytext");
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);This should work!
- CrystalKlarich-Community Member
Hi all,
I'm piggy backing off Zsolt's awesome code. I needed to copy multiple variables and static text, so I modified Zsolt's code and created a tutorial here: https://www.klarichdesigns.com/2021/10/28/sl-clipboard/. JavaScript always looks so intimidating, so hopefully this helps anybody new.
Enjoy!
- JacksonHamnerCommunity Member
http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript
It looks like they make it purposfully difficult for reasons, but it looks like someone came up with a handy work around. Check this out and see if it helps!
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
- EmilyBurnettCommunity Member
Hello Brett!
Welcome to the community! Hopefully you can get this to work using this JavaScript :)
- BrettSchlagel-eCommunity Member
That worked! Thanks for your help! Super appreciated!!
- EmilyBurnettCommunity Member
Thanks for the update Brett!
- AurélienCoussemCommunity Member
Hi everyone,
I used your (great) solution in one of my production, it works perfectly. Thanks for this.
But the problem I get is that the text copied from the variable is pasted in the clipboard without any spaces or <br>... So it is not perfect for the user.
I'm not familiar with JS, do you have any idea how I could manage a solution to get the text from the variable with the spaces etc. ?
- JacksonHamnerCommunity Member
Could you give an example of what you mean? The code is maintaining the spaces/ new lines for me.
If you need to convert the \n into </br> then switch
var text= player.GetVar("copytext");
to
var text= player.GetVar("copytext").replace(/(\r\n|\r|\n)/g, '<br />');
- AurélienCoussemCommunity Member
ok, you're right the spaces are maintened... and new lines too... Sorry for this silly question... for your info, when the pop-up appears the text appears without the new lines, so I thought it didn't took it, but when you paste it in another soft, the new lines are well displayed !
Sorry again, and thank you for your fast answer!