Update copyToClipboard.md

This commit is contained in:
Angelos Chalaris
2019-11-21 22:02:44 +02:00
committed by GitHub
parent 700a5d8c12
commit 52ef8585b5

View File

@ -3,11 +3,11 @@ title: copyToClipboard
tags: browser,string,advanced
---
⚠️ **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).
Copy a string to the clipboard.
Only works as a result of user action (i.e. inside a `click` event listener).
⚠️ **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).
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.
@ -36,4 +36,4 @@ const copyToClipboard = str => {
```js
copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.
```
```