Created
May 12, 2016 17:41
-
-
Save puresamari/6cd01e497adf8dd16c7ae40a4e94f0a6 to your computer and use it in GitHub Desktop.
copy code from a prismjs code element with linebreaks in plain es5
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
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