Skip to content

Instantly share code, notes, and snippets.

@rista404
Created April 20, 2018 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rista404/6a68668f0915d828650c42de57d8d756 to your computer and use it in GitHub Desktop.
Save rista404/6a68668f0915d828650c42de57d8d756 to your computer and use it in GitHub Desktop.
function selectText(element) {
let range
let selection
if (document.body.createTextRange) {
//ms
range = document.body.createTextRange()
range.moveToElementText(element)
range.select()
} else if (window.getSelection) {
//all others
selection = window.getSelection()
range = document.createRange()
range.selectNodeContents(element)
selection.removeAllRanges()
selection.addRange(range)
}
return selection.toString() || range.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment