JSFiddle - React, Tailwind, and code Playground

HTML

<textarea style="width:500px; height: 150px;" id="text">This is some text</textarea>
<br />
<input type="button" value="Click Me" id="button" />
<input type="button" value="Click Me 2" id="button2" />

JavaScript

function insertText(text)
{
	var $textbox = $("#text");
    var textStr  = $textbox.text();
    var startPos = $textbox[0].selectionStart;
    var endPos   = $textbox[0].selectionEnd;

    // If set to true, the selection will NOT be replaced.
    // Instead, the text will be inserted before the first highlighted character.
    if (false)
    {
    	endPost = startPos;
    }
    
    var beforeStr = textStr.substr(0, startPos);
    var afterStr  = textStr.substr(endPos, textStr.length);

    textStr = beforeStr + text + afterStr;
    $textbox.text(textStr);
    return textStr;
}

$(function () {
    $('#button').on('click', function () {
        insertText("\n\nafter clicking");    
    });
    $('#button2').on('click', function () {
        insertText("\n\nafter clicking 2");    
    });
});