JSFiddle - React, Tailwind, and code Playground

HTML

fdfdsf
<br />
fdsfsdf

JavaScript

var selectedText = "";
function getSelectedText() {
    if (typeof window.getSelection != "undefined") {
        selectedText = window.getSelection().toString();
    } else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
        selectedText = document.selection.createRange().text;
    }
}

function show_selected_text()
{
    getSelectedText();
    if(selectedText) alert(selectedText);
}

document.onmouseup = show_selected_text;
document.onkeyup = show_selected_text;