rahul kumar
rahul kumar

Reputation: 1

Working on tinymce editor but not able to set the value by using setcontent

I was using tinymce editor but the problem I was facing is when I was trying to set the value on SetContent function it was showing error like setcontet property is null and not able to store the value same script is working fine on angular project but not in vb.net

The change which i was done in my project is just hold the value in jquery function and pass that value in tinymce.activeeditor.setcontent

<script>
    function setEditorVal() {
        debugger;
        tinyMCE.activeEditor.setContent('hell0 world');            
    }
</script>

Upvotes: 0

Views: 1169

Answers (2)

M.Haris
M.Haris

Reputation: 769

maybe its a timing issue. try set timeout for the worst case.

 let tmc = tinymce.get(selector);
   if(tmc !== null){
      setTimeout(()=>{
         tmc.setContent(content);
      },1000);
   }

Upvotes: 0

Michael Fromin
Michael Fromin

Reputation: 13746

Without seeing running code I can't say for sure but the issue here is almost certainly a timing issue of when your JavaScript runs.

The TinyMCE init() function is asynchronous. When you call init() it takes some time for the process to complete. In your code example when you call activeEditor are you 100% sure there is indeed an active editor? If you call activeEditor before init() is done there is no "active editor".

The correct way to make sure that TinyMCE is fully initialized is to rely on the init event. This event is fired after TinyMCE is fully initialized and ready for interaction.

To load content via the init() you can do something like this:

tinymce.init({
  selector: "textarea",
  plugins: "advlist autolink lists ...",
  toolbar: "undo redo | bullist numlist ...",
  setup: function (editor) {
    editor.on('init', function (e) {
      //this gets executed AFTER TinyMCE is fully initialized
      editor.setContent('<p>This is content set via the init function</p>');
    });
  }
});  

Upvotes: 1

Related Questions