function getCaretPosition(el) {
    const rootDocument = document;

    if (el.selectionStart) {
        return el.selectionStart;
    } else if (rootDocument.selection) {
        // IE8
        el.focus();

        const r = rootDocument.selection.createRange();

        if (r === null) {
            return 0;
        }
        const re = el.createTextRange();
        const rc = re.duplicate();

        re.moveToBookmark(r.getBookmark());
        rc.setEndPoint("EndToStart", re);

        return rc.text.length;
    }

    return 0;
}
Sets caret position in text input.

@author http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
@param {Element} element An element to process.
@param {number} pos The selection start position.
@param {number} endPos The selection end position.