JSFiddle - React, Tailwind, and code Playground

HTML

<div>
<button id="btnCenter">
center
</button>
<br/>
<textArea id="mytextarea" style="dislplay: block;"></textArea>

</div>

JavaScript

function getSel() // javascript
{
console.log("test");
	  
    // obtain the object reference for the textarea>
    var txtarea = document.getElementById("mytextarea");
    // obtain the index of the first selected character
    var start = txtarea.selectionStart;
    // obtain the index of the last selected character
    var finish = txtarea.selectionEnd;
    //obtain all Text
    var allText = txtarea.value;
    
    // obtain the selected text
    var sel = allText.substring(start, finish);
    //append te text;
    var newText=allText.substring(0, start)+"<center>"+sel+"</center>"+allText.substring(finish, allText.length);
    
    txtarea.value=newText;
    console.log(newText);
    // do something with the selected content
}
document.getElementById("btnCenter").addEventListener("click",getSel);