JSFiddle - React, Tailwind, and code Playground

HTML

Try both selecting text and typing.<br>

<textarea onselect="updateInfo(this)" onmousedown="updateInfo(this)" onkeydown="updateInfo(this)"></textarea><br><span id="info"></span>

<script>
function updateInfo(ele) {
	var start = ele.selectionStart;
  var end = ele.selectionEnd;
  if (start!=end) {
 	  document.getElementById("info").innerHTML = "The selection is between "+start+" and "+end+".";
  }
  else {
  	document.getElementById("info").innerHTML = "The caret is at "+start+".";
  }
}
</script>