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.
Setting the cursor position You can also hold down shift and then move the cursor with the arrow keys on the keyboard to select content. It has the same effect. The TinyMCE bookmarks this location with the Bookmark Manager API, and then loads the selected location later.
You can also try out the save shortcut key. After writing content into your editor, use the command key + s or control key + s. The page reloads, and your file saves to the “database”.
You should use the command mceInsertContent
. See the TinyMCE documentation.
tinymce.activeEditor.execCommand('mceInsertContent', false, "some text");
The above answer is good, but it is worth pointing out that this can be used to insert any HTML.
For example:
tinymce.activeEditor.execCommand('mceInsertContent', false, " <b>bolded text</b> ");
will insert bolded text at the current cursor location.
Some other interesting observations:
mceInsertRawHTML also works, but tends to put the cursor at the beginning of the current line in my version of tinyMCE, but ymmv.
mceReplaceContent works as well, but in my case did not work well when the cursor was at the end of the current content.
Again, see the documentation for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With