Skip to content

Instantly share code, notes, and snippets.

@puresamari
Created May 12, 2016 17:41
Show Gist options
  • Save puresamari/6cd01e497adf8dd16c7ae40a4e94f0a6 to your computer and use it in GitHub Desktop.
Save puresamari/6cd01e497adf8dd16c7ae40a4e94f0a6 to your computer and use it in GitHub Desktop.
copy code from a prismjs code element with linebreaks in plain es5
var codes = document.querySelectorAll('pre');
function copyCode(e) {
var text = this.textContent || this.innerText,
temp = document.createElement('textarea');
document.body.appendChild(temp);
temp.value = text;
temp.select();
document.execCommand('copy');
temp.remove();
}
for (var i = 0; i < codes.length; i++) {
var codeElem = codes[i];
codeElem.addEventListener('click', copyCode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment