JSFiddle - React, Tailwind, and code Playground

HTML

<textarea id="Editor">asdadasda dad sadas dad</textarea>
<input type="button" id="p" value="get text"/>

JavaScript

$('#p').on('click', function () {
   var textComponent = document.getElementById('Editor');
   var selectedText; 
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos) 
  alert("You selected: " + selectedText.length);
});