JSFiddle - React, Tailwind, and code Playground
HTML
<textarea>Some text</textarea>
<textarea>Some text more text</textarea>
<div></div>
<p>Some random text on the page</p>
JavaScript
var selectedTextarea = "";
$("textarea").select(function() {
selectedTextarea = getSelectedText();
$("div").text(selectedTextarea);
});
$("textarea").on("blur focus keydown mousedown", function() {
if(selectedTextarea.length > 0){
$("div").text("");
selectedTextarea = "";
}
});
function getSelectedText()
{
var txt = "";
var textComponent = document.getElementById('Editor');
if (window.getSelection)
{
txt = window.getSelection();
//for FF
if (document.activeElement.nodeName == "TEXTAREA"){
ta = document.activeElement;
txt = ta.value.substring(ta.selectionStart, ta.selectionEnd);
}
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else if (textComponent.selectionStart != undefined)
{
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
txt = textComponent.value.substring(startPos, endPos);
}
return txt.toString();
}