Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
In this article, I will be explaining in depth how the copyToClipboard snippet from 30 seconds of code works. You can find the source code for it and a ton of other useful methods in the project’s repository.
30 seconds of code: Javascript snippets that you can understand in 30 seconds or lessCore functionality
One thing that comes up quite often in website building is the ability to copy some text to clipboard, without the user selecting it or hitting the appropriate key combination on their keyboard. Javascript can easily do this in five short steps:
- Create a <textarea> element to be appended to the document. Set its value to the string that we want to copy to the clipboard.
- Append said <textarea> element to the current HTML document.
- Use HTMLInputElement.select() to select the contents of the <textarea> element.
- Use Document.execCommand('copy') to copy the contents of the <textarea> to the clipboard.
- Remove the <textarea> element from the document.
The simplest version of this method should look like this:
Bear in mind that this method will not work everywhere, but only as a result of a user action (like inside a click event listener), due to the way Document.execCommand() works.
Making the appended element invisible
If you try out the above method, you will probably see some flashing, when the <textarea> element is appended and then removed. This problem is especially bad for people on screenreaders, as it can cause some really annoying issues. So, the next logical step is to use some CSS to make this element invisible and make it readonly in case the users try to mess with it:
Saving and restoring the original document’s selection
The final consideration is that the user might have already selected some content on the HTML document, so it would be nice to not remove anything they might have selected. Luckily, we can now use some modern Javascript methods and properties like DocumentOrShadowRoot.getSelection(), Selection.rangeCount, Selection.getRangeAt(), Selection.removeAllRanges() and Selection.addRange() to save and restore the original document selection. Here’s the final, annotated code implementing these improvements:
And that’s pretty much all there is to it. In less than 20 lines of code, we have created one of the most commonly needed methods in frontend development.
If you like this article, give it a clap (or fifty) and remember to check out 30 seconds of code for more useful code snippets for your Javascript projects!
Copying text to clipboard with JavaScript was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.