+- Title: copyToClipboard
+- Tags: browser,string,event,advanced
+- [downgraf](https://www.downgraf.com/)
+Copies a string to the clipboard.
+Only works as a result of user action (i.e. inside a `click` event listener).
+- Create a new `<textarea>` element, fill it with the supplied data and add it to the HTML document.
+- Use `Selection.getRangeAt()`to store the selected range (if any).
+- Use `Document.execCommand('copy')` to copy to the clipboard.
+- Remove the `<textarea>` element from the HTML document.
+- Finally, use `Selection().addRange()` to recover the original selected range (if any).
+- ⚠️ **NOTICE:** The same functionality can be easily implemented by using the new asynchronous Clipboard API, which is still experimental but should be used in the future instead of this snippet. Find out more about it [here](https://github.com/w3c/clipboard-apis/blob/master/explainer.adoc#writing-to-the-clipboard).
+const copyToClipboard = str => {
+ const el = document.createElement('textarea');
+ el.setAttribute('readonly', '');
+ el.style.position = 'absolute';
+ el.style.left = '-9999px';
+ document.body.appendChild(el);
+ document.getSelection().rangeCount > 0
+ ? document.getSelection().getRangeAt(0)
+ document.execCommand('copy');
+ document.body.removeChild(el);
+ document.getSelection().removeAllRanges();
+ document.getSelection().addRange(selected);
+copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.