Get Selected Text in Javascript

Posted by Joe Enos on June 10, 2021 · 1 min read
function getSelectedText() {
if (window.getSelection) {
return window.getSelection();
}
if (document.getSelection) {
return document.getSelection();
}
if (document.selection) {
return document.selection.createRange().text;
}
return "";
}