Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 883
Ruba Avatar asked Nov 15 '12 08:11

Ruba


People also ask

How do you get the cursor position in TinyMCE editor?

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.

How do you save on TinyMCE text editor?

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”.


2 Answers

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

tinymce.activeEditor.execCommand('mceInsertContent', false, "some text"); 
like image 140
Magus Avatar answered Sep 20 '22 13:09

Magus


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.

like image 28
ajl Avatar answered Sep 19 '22 13:09

ajl