Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Text at current position TINYMCE IE9

How to insert text into a tinyMce edtitor at the current position. It works perfect in Chrome, FF, Safari, but in IE it always starts at the top of the textarea. I currently do the following:

tinyMCE.execCommand('mceInsertContent',false, 'blabla this is tekst');

I tried with focus, other commands, nothing works :(.

like image 310
iBoonZ Avatar asked Dec 04 '22 01:12

iBoonZ


1 Answers

Thanks for the help, but nothing worked. I found my own solution, and I felt like sharing this was needed, because I only found outdated solutions, that did not work :(.

The solution:

In your TinyMCE init add following code:

    tinyMCE.init({
// General options
        elements: elementid,
        setup: function (ed) {
            ed.onKeyUp.add(function (ed, e) {
                actualCaretPositionBookmark = tinyMCE.activeEditor.selection.getBookmark();
            });
            ed.onClick.add(function (ed, e) {
                actualCaretPositionBookmark = tinyMCE.activeEditor.selection.getBookmark();
            }); 
        }

Where "actualCaretPositionBookmark" is a global variable in javascript that can be accesed by all your javascript code

Then when you want to add text into your TinyMCE editor via javascript, add following code:

                if (tinymce.isIE) {
                tinyMCE.activeEditor.selection.moveToBookmark(actualCaretPositionBookmark);
                tinyMCE.execCommand('mceInsertContent',false, 'My new text'); 

            }else {
                tinyMCE.execCommand('insertHTML',false, 'My new text'); 
            }
like image 145
iBoonZ Avatar answered Dec 26 '22 12:12

iBoonZ