JSFiddle - React, Tailwind, and code Playground
HTML
<textarea id="txt" rows="15" cols="70">There is some text here.</textarea>
<input type="button" id="btn" value="OK" />
JavaScript
var btn = document.getElementById("btn"),
txt = document.getElementById("txt");
if (btn && txt) {
btn.addEventListener("click", function () {
var caretPos = document.getElementById("txt").selectionStart,
textAreaTxt = txt.value,
txtToAdd = "stuff";
txt.value = textAreaTxt.substring(0, caretPos)
+ txtToAdd
+ textAreaTxt.substring(caretPos);
txt.focus();
txt.selectionStart = caretPos + txtToAdd.length;
txt.selectionEnd = caretPos + txtToAdd.length;
});
}