JSFiddle - React, Tailwind, and code Playground

by Emanuel Vecchio

HTML

<div>Here is some text</div>

JavaScript

$(window).mouseup(function() {
        var text = getSelectedText()
        if (text && text.length > 0) {
            alert(text);
        }
    });

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