最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reset tinyMce content - Stack Overflow

programmeradmin1浏览0评论

I'm trying to reset tinymce content.

On the beginning there is some 'A content'. User changed it to some 'B content', but he don't want to save it, so he can click 'cancel' button and whole content is back to 'A' version.

A content is text saved earlier, so it's not constant. How to reset tinyMce text to original one? Html:

<div id="someDiv">Content A</div>

Would be nice to see something like this. If content was modified, cancel button will reset content to original:

if($('#someDiv').tinymce().isDirty()) {:
    $('#someDiv').tinymce().reset();
}

I'm trying to reset tinymce content.

On the beginning there is some 'A content'. User changed it to some 'B content', but he don't want to save it, so he can click 'cancel' button and whole content is back to 'A' version.

A content is text saved earlier, so it's not constant. How to reset tinyMce text to original one? Html:

<div id="someDiv">Content A</div>

Would be nice to see something like this. If content was modified, cancel button will reset content to original:

if($('#someDiv').tinymce().isDirty()) {:
    $('#someDiv').tinymce().reset();
}
Share Improve this question asked Mar 18, 2013 at 16:10 imclickingmaniacimclickingmaniac 1,5731 gold badge17 silver badges30 bronze badges 5
  • 2 Yes, bravo CBroe. You know that i've visited almost all the results from few first pages of this query before i posted here? If you will find there answer for my question, then post, and don't treat me like an idiot! – imclickingmaniac Commented Mar 19, 2013 at 8:47
  • I can’t know that you did any research of your self before if you don’t mention that in any way. – C3roe Commented Mar 19, 2013 at 9:11
  • Well, sorry. I also don't like when ppl don't search before question, so I understand your intention. All results I found are about clearing editor, but not about reset to its beginning state. Also found few examples that doesn't work :( So I'm still waiting for help. – imclickingmaniac Commented Mar 19, 2013 at 9:29
  • 1 I doubt that a simple “reset” is even possible – since these editors actually manipulate DOM content on the fly. I guess one would have to “save”/clone the original DOM node the editor is called upon before doing so – and then have the actual, user-manipulated DOM replaced by this copy of the original nodes when one wants to “reset” everything to its original state. – C3roe Commented Mar 19, 2013 at 9:41
  • Yes. This is what i have done now. Because this is div and not form element the js reset will not help. I had little hope that there is some built in function to reset tinyMCE content and I just can't find it :( – imclickingmaniac Commented Mar 19, 2013 at 10:16
Add a ment  | 

1 Answer 1

Reset to default 3

This can be easily done.

You need to add this (using the setup parameter) to your configuration:

tinyMCE.init({
   ...
   setup : function(ed) {
      ed.onInit.add(function(ed, evt) {
          ed.init_content = ed.getContent();
      });
   }
});

on buttonclick you call the following to reset the editor content

var ed = tinymce.get('your_editor_id');
ed.setContent(ed.init_content);

EDIT - For tinymce 4.x the syntax for attaching editor events has changed and is now:

tinymce.init({
    ...
    setup: function (ed) {
        ed.on('init', function () {
            ...
        });
    }
});
发布评论

评论列表(0)

  1. 暂无评论