Last active
September 29, 2020 00:09
-
-
Save itsabdessalam/4dcff3a5e3ebad6cd8afe539c67beb2c to your computer and use it in GitHub Desktop.
Copy to clipboard
This file contains 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
// if code snippet is inside input or textarea tags | |
function copy() { | |
var codeSnippet = document.querySelector("#code-snippet"); | |
codeSnippet.select(); | |
document.execCommand("copy"); | |
} | |
document.querySelector("#copy").addEventListener("click", copy); | |
// if code snippet is inside pre or code tags | |
function copy() { | |
var codeSnippet = document.querySelector("#code-snippet"); | |
var textarea = document.createElement('textarea'); | |
textarea.value = codeSnippet.innerText || codeSnippet.textContent; | |
document.body.appendChild(textarea); | |
textarea.select(); | |
document.execCommand("copy"); | |
document.body.removeChild(textarea); | |
} | |
document.querySelector("#copy").addEventListener("click", copy); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment