Skip to content

Instantly share code, notes, and snippets.

@JCThePants
Created December 5, 2015 03:18
Show Gist options
  • Save JCThePants/00506908138c1899a9d6 to your computer and use it in GitHub Desktop.
Save JCThePants/00506908138c1899a9d6 to your computer and use it in GitHub Desktop.
Select all text in an HTML element
function selectAllText(elem) {;
var range = document.createRange();
range.setStart(elem, 0);
var i = 0;
while (i < 999999) { // could have used 'true', but I don't like the infinite loop
try {
range.setEnd(elem, i++);
} catch (err) {
break;
}
}
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment