Skip to content

Instantly share code, notes, and snippets.

@GitHub30
Created November 6, 2019 16:59
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 GitHub30/28f88a2406212caab968dc4109f957ba to your computer and use it in GitHub Desktop.
Save GitHub30/28f88a2406212caab968dc4109f957ba to your computer and use it in GitHub Desktop.
function getSelectedText() {
const activeElement = document.activeElement
if(activeElement && ['input', 'textarea'].includes(activeElement.tagName.toLowerCase())) {
return activeElement.value.substring(activeElement.selectionStart, activeElement.selectionEnd)
} else {
return getSelection().toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment