Last active
August 29, 2015 14:03
-
-
Save nwinkler/5110fc7f1ec6caaeea27 to your computer and use it in GitHub Desktop.
Select text range in browser (outside of input fields)
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
/** | |
* From http://stackoverflow.com/a/987376/1228454 | |
* | |
* Cross-browser function for selecting text ranges. | |
* | |
* @param element A jQuery element that has the text. | |
*/ | |
function selectText(element) { | |
var doc = document, | |
range, selection; | |
if (doc.body.createTextRange) { //ms | |
range = doc.body.createTextRange(); | |
range.moveToElementText(element[0]); | |
range.select(); | |
} else if (window.getSelection) { //all others | |
selection = window.getSelection(); | |
range = doc.createRange(); | |
range.selectNodeContents(element[0]); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment