JSFiddle - React, Tailwind, and code Playground
HTML
<textarea name="" id="texto" cols="30" rows="10">Texto de exemplo</textarea>
JavaScript
function showSelection(textComponent) {
var selectedText;
// IE version
if (document.selection != undefined) {
textComponent.focus();
var sel = document.selection.createRange();
selectedText = sel.text;
}
// Mozilla version
else if (textComponent.selectionStart != undefined) {
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
selectedText = textComponent.value.substring(startPos, endPos)
}
return selectedText;
}
var textarea = document.getElementById('texto');
textarea.addEventListener('mouseup', function () {
var textoSelecionado = showSelection(this);
if (textoSelecionado) alert(textoSelecionado);
});