Created
September 6, 2019 12:19
-
-
Save codebubb/e6ecaa76b1843520d2e9768cceb83b37 to your computer and use it in GitHub Desktop.
Copy to clipboard in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <input id="inputText" value="Text to copy"> | |
| <button id="copyInputText">Copy</button> | |
| <span id="elementText">More Text to copy</span> | |
| <button id="copyElementText">Copy</button> | |
| <script> | |
| const execCopy = () => { | |
| inputText.select(); | |
| document.execCommand('copy') | |
| }; | |
| const writeToClipboard = () => { | |
| window.navigator.clipboard.writeText(elementText.innerText); | |
| }; | |
| copyInputText.addEventListener('click', execCopy); | |
| copyElementText.addEventListener('click', writeToClipboard); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment