JSFiddle - React, Tailwind, and code Playground

HTML

<div id="editor" contenteditable><p><a href="../">forcus this editor and type Enter</a></p></div>
<pre id="source"></pre>

JavaScript

var editor = document.getElementById("editor");
var pre = document.getElementById("source");
editor.addEventListener("focus", () => {
  setTimeout(() => {
    let text = editor.firstChild.firstChild.firstChild;
    window.getSelection().collapse(text, text.length);
  }, 100);
}, { once: true });
editor.addEventListener("input", () => {
  pre.textContent = editor.innerHTML;
});