JSFiddle - React, Tailwind, and code Playground

HTML

<html>
    <head>
        <script type="text/javascript">
            function getSelected() {
                var text = "";
                if (window.getSelection
                && window.getSelection().toString()
                && $(window.getSelection()).attr('type') != "Caret") {
                    text = window.getSelection();
                    return text;
                }
                else if (document.getSelection
                && document.getSelection().toString()
                && $(document.getSelection()).attr('type') != "Caret") {
                    text = document.getSelection();
                    return text;
                }
                else {
                    var selection = document.selection && document.selection.createRange();
            
                    if (!(typeof selection === "undefined")
                    && selection.text
                    && selection.text.toString()) {
                        text = selection.text;
                        return text;
                    }
                }
            
                return false;
            }
        </script>
    </head>
    <body>
        <h2>HELLO WORLD</h2>
        <div> this is some text</div>
        <p> mmmmmtext</p>
        <input type="button" value="get text" onclick="alert(getSelected())"/>
    </body>
</html>