JSFiddle - React, Tailwind, and code Playground

HTML

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<div id="main-input" contenteditable="true">Hello world!</div>

JavaScript

$('div[contenteditable="true"]').bind("mouseup keyup touchend", function() {
    lastCaretIndex = getSelectionRange();
});

    function getSelectionRange() {
        var sel;
        if (window.getSelection) {
            sel = window.getSelection();

            console.log(sel); // this doesn't print anything event empty string

            if (sel.rangeCount) {
                return sel.getRangeAt(0);
            }
        } else if (document.selection) {
            return document.createRange();
        }

        return null;
    }