Skip to content Skip to sidebar Skip to footer

Summernote - On Focus, Insert Cursor At The End Of Editor

When the Summernote text editor initializes with the focus property set to true, the editor gains focus but places the cursor at the beginning of the editor. My preference would be

Solution 1:

Here is the solution I've modified to work for me:

$.fn.extend({
    placeCursorAtEnd: function() {
        // Places the cursor at the end of a contenteditable container (should also work for textarea / input)if (this.length === 0) {
            thrownewError("Cannot manipulate an element if there is no element!");
        }
        var el = this[0];
        var range = document.createRange();
        var sel = window.getSelection();
        var childLength = el.childNodes.length;
        if (childLength > 0) {
            var lastNode = el.childNodes[childLength - 1];
            var lastNodeChildren = lastNode.childNodes.length;
            range.setStart(lastNode, lastNodeChildren);
            range.collapse(true);
            sel.removeAllRanges();
            sel.addRange(range);
        }
        returnthis;
    }
});

Which originally came from this post: https://stackoverflow.com/a/6249440/2921200 and then I modified to work with jQuery & specifically against Summernote.

Post a Comment for "Summernote - On Focus, Insert Cursor At The End Of Editor"