JSFiddle - React, Tailwind, and code Playground

HTML

<textarea rows="5" cols="80" id="input">One two three</textarea>
<textarea rows="5" cols="80" id="selection"></textarea>
<input type="button" onclick="copySelected()" value="copy selected">

JavaScript

function getSelectedText(el) {
    if (typeof el.selectionStart == "number") {
        return el.value.slice(el.selectionStart, el.selectionEnd);
    } else if (typeof document.selection != "undefined") {
        var range = document.selection.createRange();
        if (range.parentElement() == el) {
            return range.text;
        }
    }
    return "";
}

function copySelected() {
    var srcTextarea = document.getElementById("input");
    var destTextarea = document.getElementById("selection");
    destTextarea.value = getSelectedText(srcTextarea);
}