8

I try to catch the Blur and Focus Events of a tinyMce Editor.

I found following way for this.

            ed.onInit.add(function(ed) {
                tinyMCE.execCommand('mceRepaint');

                var dom = ed.dom;
                var doc = ed.getDoc();

                if (o.onblurtopics) {
                    tinymce.dom.Event.add(doc, 'blur', function(e) {
                        alert("blur");
                    });
                }
                if (o.onfocustopics) {
                    tinymce.dom.Event.add(doc, 'focus', function(e) {
                        alert("focus");
                    });
                }

            });

This works fine, but only in Firefox. When I try this in current Chromium or IE8 it has no effect.

Does anyone has a suggestion?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Johannes
  • 2,060
  • 3
  • 17
  • 27
  • Please visit this link; i hope that will help you http://stackoverflow.com/questions/5593019/tinymce-blur-event/12800840 – Mudaser Ali Oct 24 '12 at 06:36

3 Answers3

10

Use

tinymce.dom.Event.add(s.content_editable ? ed.getBody() : (tinymce.isGecko ? ed.getDoc() : ed.getWin()), 'blur', function(e) {
    alert('blur');
}
Fabio
  • 18,856
  • 9
  • 82
  • 114
Rajesh Taneja
  • 101
  • 1
  • 3
5

Seems overly complicated to me, this should work:

        editor.onInit.add(function(editor) {
            tinymce.dom.Event.add(editor.getBody(), "focus", function(e) {
                parent.console.log('focus');
            });
        });

        editor.onInit.add(function(editor) {
            tinymce.dom.Event.add(editor.getBody(), "blur", function(e) {
                parent.console.log('blur');
            });
        });
Luke Madhanga
  • 6,871
  • 2
  • 43
  • 47
Kristoffer
  • 362
  • 2
  • 10
  • 2 problems here. A) editor.getBody() doesn't work everywhere. B) console.log won't work since the log falls into the tinyMCE iframe. It appears to not work. – Parris Sep 26 '13 at 21:20
2

You could use jQuery to take care of the blur/focus (jQuery then will take care of the browser dependent stuff).

Update: It works! Here is the tinymce fiddle: http://fiddle.tinymce.com/ageaab/1

And here is the code:

<script type="text/javascript">
tinymce.init({
    selector: "textarea",
    plugins: [
        "advlist autolink lists link image charmap print preview anchor",
        "searchreplace visualblocks code fullscreen",
        "insertdatetime media table contextmenu paste"
    ],
    toolbar: "bold italic",
    setup : function(ed) {
        ed.on('init', function()
        {
            $(ed.getBody()).on('blur', function(e) {
                console.log('blur');
            });
            $(ed.getBody()).on('focus', function(e) {
                console.log('focus');
            });
        });
    }

});
</script>

<form method="post" action="dump.php">
    <textarea name="content"></textarea>
</form>
Thariama
  • 50,002
  • 13
  • 138
  • 166