Skip to content

Instantly share code, notes, and snippets.

@mcansh
Last active September 8, 2017 00:32
Show Gist options
  • Save mcansh/d79d5d557188af7097b6eef2a00ddd93 to your computer and use it in GitHub Desktop.
Save mcansh/d79d5d557188af7097b6eef2a00ddd93 to your computer and use it in GitHub Desktop.
copy to clipboard
// copy a string
function copyToClipboard(string) {
const input = document.createElement('input')
input.type = 'text';
input.value = string;
document.body.appendChild(input);
input.select();
document.execCommand('copy');
input.remove();
}
copyToClipboard('copied 👌');
// want to copy an elements text content?
function copyToClipboard(element) {
const string = element.textContent.trim();
const input = document.createElement('input');
input.type = 'text';
input.value = string;
document.body.appendChild(input);
input.select();
document.execCommand('copy');
input.remove();
}
copyToClipboard(document.querySelector('.repository-meta-content'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment