Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Created October 4, 2013 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daniel-Hug/6821290 to your computer and use it in GitHub Desktop.
Save Daniel-Hug/6821290 to your computer and use it in GitHub Desktop.
Select text with Javascript
codeElements = document.getElementsByTagName('code');
for (var i = codeElements.length; i--;) {
codeElements[i].onclick = function() {
var range;
if (document.selection) {
range = document.body.createTextRange();
range.moveToElementText(this);
range.select();
}
else if (window.getSelection) {
range = document.createRange();
range.selectNode(this);
window.getSelection().addRange(range);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment