Created
September 17, 2013 19:21
-
-
Save gubatron/6599366 to your computer and use it in GitHub Desktop.
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 selectText(element) { | |
var doc = document | |
, text = doc.getElementById(element) | |
, range, selection | |
; | |
if (doc.body.createTextRange) { //ms | |
range = doc.body.createTextRange(); | |
range.moveToElementText(text); | |
range.select(); | |
} else if (window.getSelection) { //all others | |
selection = window.getSelection(); | |
range = doc.createRange(); | |
range.selectNodeContents(text); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
} | |
} |
wow, glad you found it and it still works, had totally forgotten about this gist.
Cheers!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a great method for selecting text if you use a non-input element as a textbox. Thanks