Forum Discussion
How to copy text to the clipboard?
In my advanced Storyline session at the ATD TechKnowledge conference people asked me if there's a way to copy text from a variable to the clipboard programmatically, so the user can paste it somewhere.
Yes, try this (using JavaScript):
https://www.rabbitoreg.com/examples/p99/#/lessons/NPQ2NG0PlDgwjy_ZLbmw6t0PPaoWFEB6
- CrystalKlarich-Community Member
Thank you for sharing the link, Zsolt! I needed to string together several variables and static text, so I fiddled with the code. For anyone interested, I have a tutorial and the modified code here: https://www.klarichdesigns.com/2021/10/28/sl-clipboard/.
Thanks much!
- ZsoltzoolahamazCommunity Member
Oh, okay. The question was how.. Try this after the copy command:
setTimeout(function() {textArea.remove();}, 1000);
1000 is a second delay. You can adjust that if you want. - MargoBedford-2fCommunity Member
I got this to work in Storyline, but what about Rise? You definitely have it working in Rise on your site: https://www.rabbitoreg.com/examples/p99/#/lessons/NPQ2NG0PlDgwjy_ZLbmw6t0PPaoWFEB6 How did you do it??
I'm glad you were able to create a solution, Crystal. I appreciate you chiming in to share, and welcome to E-Learning Heroes. 😊
- MartinSinclair-Community Member
Thanks got this Zsolt - just built an troubleshooter that sits in an iframe pop-up within one of our tools, and this JS copies some text across to an email which can then be sent straight to our IT dept when things go wrong. Super useful!
Last piece of the jigsaw. When the text is copied, in the top left of the browser it shows a text box with the copied text. I believe this is because the JS creates the textboc then copies its contents. So how can I get the textbox to delete after this has taken place?
Thanks for any help! - ZsoltzoolahamazCommunity Member
Hi Martin,
Yes, a new text box is created with the text, and the content is copied to the clipboard. We do not need that text box after. It can be deleted. - YuYingLinCommunity Member
Hello! Thank you for this function. I am planning to use it for my e-learning project. There's one question: if I want to have several lines in the text, how can I do that?
For example, there's a code I want my learners to copy like the following:
cd git-fundamental-repo01
echo "hello world" > file1.txt
cat file1.txt
How can I make it happen in just one click?
- AmallieAlegreCommunity Member
Hi Zsolt thank you so much for sharing this! I was hoping to ask if you have a way as well to paste text to a text entry with a live word count. Currently, I only configure my word count if the user starts typing. However when a text is copied and pasted in the text entry-- it does not recognize the word count anymore. Appreciate your help on this. Attached for your reference.
- ChristinaClark-Community Member
Hi Zsolt, thank you for sharing your link. Super helpful.
I have linebreaks in the text to be copied that I want to preserve when it's copied to the clipboard. For example
John Smith
Jane Doe
Peter White
In the input box for the storyline variable, I have no option to include linebreaks and can only render "John Smith Jane Doe Peter White". I've tried adding <br>in the text where I want the linebreak to appear, but that's not working. The copied text also includes the <br> rather than interpreting it as code.
Is there a way to preserve linebreaks in the output text when using your code to copy to clipboard?