JSFiddle - React, Tailwind, and code Playground

by jakie8

HTML

<div contenteditable="true">
    <p>Random Text</p>
</div>
<input type="button" onclick="getSelectedElement()" value="Click"> <pre id="res"></pre>

JavaScript

var $ = function (i) {
    return document.getElementById(i);
},

getSelectedElement = function () {
    $('res').innerText = 'Hurry, click within the text above!';
    setTimeout(function () {
        
        var selectedElement;
        
        if(window.getSelection){
            selectedElement = window.getSelection().anchorNode.parentNode;
        } else {
            selectedElement = document.selection.typeDetail;
        }


        console.log(selectedElement);

    }, 1000);
}