Created
October 24, 2018 14:04
-
-
Save jsuryahyd/2da8802e57d632bffc06afe9b70aa780 to your computer and use it in GitHub Desktop.
Select text with javascript on the browser.
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
//dont remember, where got this from, but, | |
function selectText(id) { | |
var node = document.getElementById(id); | |
if (document.body.createTextRange) { | |
const range = document.body.createTextRange(); | |
range.moveToElementText(node); | |
range.select(); | |
} else if (window.getSelection) { | |
const selection = window.getSelection(); | |
const range = document.createRange(); | |
range.selectNodeContents(node); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
} else { | |
console.warn("Could not select text in node: Unsupported browser."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment