Created
December 5, 2015 03:18
-
-
Save JCThePants/00506908138c1899a9d6 to your computer and use it in GitHub Desktop.
Select all text in an HTML element
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
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