Skip to content

Instantly share code, notes, and snippets.

@peacefulseeker
Last active November 27, 2017 16:24
Show Gist options
  • Save peacefulseeker/a50eae4ba2dc7bdc537565abe8a5b512 to your computer and use it in GitHub Desktop.
Save peacefulseeker/a50eae4ba2dc7bdc537565abe8a5b512 to your computer and use it in GitHub Desktop.
JavaScript - return the current selection
function getSelected() {
if (window.getSelection) {
return window.getSelection().toString();
} else if (document.getSelection) {
return document.getSelection().toString();
} else {
var selection = document.selection && document.selection.createRange();
if (selection.text) {
return selection.text.toString();
}
return "";
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment