Javascript get highlighted selected text

Posted by Joe Enos on May 12, 2021 · 1 min read

function getSelText(){
if (window.getSelection) {
return window.getSelection();
} else if (document.getSelection) {
return document.getSelection();
} else if (document.selection) {
return document.selection.createRange().text;
}

return "";
}