Mari Selvan
Mari Selvan

Reputation: 3802

How to Destroy and Reinitialize CKEDITOR Again?

I use the below code to destroy the editor instance.

editor.destroy();

After this, I try to initialize CKEditor and set content using the below code.

CKEDITOR.replace('editor1');
CKEDITOR.instances['editor1'].setData("MY HTML DATA");

But when I am doing like this only the empty HTML page is Shown.

Empty page

How can I do this in a Correct Way?

Upvotes: 0

Views: 5614

Answers (2)

j.swiderski
j.swiderski

Reputation: 2445

Operations you have described require time to complete thus please use events to control when editor gets created and destroyed:

Upvotes: 0

SAMUEL
SAMUEL

Reputation: 8552

If i understand you correctly, following fiddle will help you in initializing and destroying ckeditor.

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <title>CKEditor</title>
    <script src="https://cdn.ckeditor.com/4.8.0/standard/ckeditor.js"></script>
  </head>

  <body>
    <div name="editor1">TEST CONTENT</div>
    <button id="toogleEditor">
    TOOGLE EDITOR
    </button>
  </body>

</html>

Here is the JS

var editorInstance;
     document.getElementById('toogleEditor').addEventListener('click',function() {
      if (CKEDITOR) {
        if (editorInstance) {
            editorInstance.destroy();
          editorInstance = undefined;
        } else {
          editorInstance = CKEDITOR.replace('editor1');
          editorInstance.setData("MY HTML DATA");
        }
      }
    })

Fiddle

Upvotes: 1

Related Questions