JSFiddle - React, Tailwind, and code Playground

HTML

<div id="messageBox" contenteditable="true" class="message"></div>
<input type='button' class='btn' value='button'>

JavaScript

function placeCaretAtEnd(el) {
    el.focus();
    if (typeof window.getSelection != "undefined"
            && typeof document.createRange != "undefined") {
        var range = document.createRange();
        range.selectNodeContents(el);
        range.collapse(false);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (typeof document.body.createTextRange != "undefined") {
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.collapse(false);
        textRange.select();
    }
}

$('.btn').click(function() {
    
    $('#messageBox').html('ok');

    placeCaretAtEnd($('#messageBox').get(0));
});