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:

Copy to Clipboard JS

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!

26 Replies
Jackson Hamner

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);
Brett Schlagel

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!

Aurélien Coussement

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. ?

Aurélien Coussement

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!

copy/paste

ignacio vazquez

Hi I’m trying to use a button to copy a variable directly to the clip board and I have not had much success.

“copytext” is the variable I am using as an input in articulate.


This is the java script I added to the articulate button:


var player = GetPlayer();
var vtext= player.GetVar("copytext");
holdtext.innerText=vtext;
Copied=holdtext.createTextRange();
Copied.execCommand("Copy");

And I added this to the story.html file:
<textarea id="holdtext" style="display: none;"></textarea>

any thoughts on how I can fix this?

Holly MacDonald

I am wondering if it's possible to use javascript to copy supplied text to the clipboard. I have a series of Storyline modules with a workbook that they will be writing in and in the Storyline module I would like to have a button that would copy text from the module (not a text entry or select text function), is this possible?

PS I'm a javascript novice.

Holly MacDonald

Hi Matthew - I've just tested this and what ends up getting copied onto the clipboard is the javascript:

var player = GetPlayer();
var text= player.GetVar("copytext");
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);

not the default text I put in the text variable. Do I need to put that text in the javascript?

If you have any insights, let me know!