JSFiddle - React, Tailwind, and code Playground

HTML

<div>Here is some text</div>

JavaScript

$('div').mouseup(function() {
        alert(getSelectedText());
    });

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