Search code examples
javascriptjquerytinymcecursor-position

Inserting text in TinyMCE Editor where the cursor is


I've been trying to insert text into the TinyMCE Editor at the focused paragraph element (<p>) exactly where the cursor is but got no luck!!

var elem = tinyMCE.activeEditor.dom.get('tinymce');
var child = elem.firstChild;
while (child) {
    if (child.focused) {
        $(child).insertAtCaret("some text");
    }
    child = child.nextSibling;
}

If anyone has any idea on how to solve this I'll be very thankful.


Solution

  • You should use the command mceInsertContent. See the TinyMCE documentation.

    tinymce.activeEditor.execCommand('mceInsertContent', false, "some text");