Skip to content

Instantly share code, notes, and snippets.

@itsabdessalam
Last active September 29, 2020 00:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save itsabdessalam/4dcff3a5e3ebad6cd8afe539c67beb2c to your computer and use it in GitHub Desktop.
Save itsabdessalam/4dcff3a5e3ebad6cd8afe539c67beb2c to your computer and use it in GitHub Desktop.
Copy to clipboard
// 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