Copy Text from Course to Clipboard

Aug 04, 2022

Hello! I'm trying to allow the learner to click a button which will copy a URL to their clipboard. Which in turn will allow them to paste it into a word document or a web browser. I have the JavaScript set up and it works great.

Here's the script I'm using:

var player = GetPlayer();
var text = player.GetVar("MyTextVariable");
copyFunction (text);

function copyFunction(tt) {

const copyText = tt;
const textArea = document.createElement('textarea');
textArea.textContent = copyText;
document.body.append(textArea);
textArea.select();
document.execCommand("copy");
alert("Copied to clipboard!");
}

 

I have changed the MyTextVariable to the name of my variable in SL.

I have changed (text) to the URL that should be copied.

But when I reuse that script and change the variable and URL on a different slide both slides copy the new URL instead of the one in the script. What am I doing wrong?

Thanks.

1 Reply