JSFiddle - React, Tailwind, and code Playground

HTML

<div id="xyz" contenteditable="true">Hello world</div>
<br>
<br>
    <input id="btn" type="button" value="get selection" />
<br>
<br>
<span class="status">Selected text : </span>

JavaScript

function yourFunction() {
    if (window.getSelection) {
        var selectionRange = window.getSelection();
        $('.status').text(selectionRange.toString());
    }
}

$('#btn').click(function () {
    yourFunction();
})