Inserting text in TinyMCE Editor where the cursor is

Ruba picture Ruba · Nov 15, 2012 · Viewed 41.8k times · Source

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.

Answer

Magus picture Magus · Nov 15, 2012

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

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