kneidels
kneidels

Reputation: 914

Correctly destroying a ckeditor instance

Using version 4.4.7

my CKEDITOR loads in a modal window that opens in the page. I noticed that when i re-open the modal, the editor does not load. The solution was to destroy the instance, and then re-apply it on the textarea.

But i can't seem to destroy it correctly. Based on this answer I have tried all the following attempts, but i keep getting an error:

TypeError: a is null
http://domaim/ckeditor/ckeditor.js
Line 778 

I have tried the following (for the sake of simplicity, am combining all the attempts in one code block, but they were tried one by one):

var editor = CKEDITOR.instances.mail_message;
    if (editor) {
        console.log('instance exists');

        // ATTEMPT 1: 
        if (CKEDITOR.instances.mail_message) CKEDITOR.instances.mail_message.destroy();
        // --------------------------------------


         // ATTEMPT 2:
         editor.destroy(true); 
         // -------------------------------------


         // ATTEMPT 3:
         CKEDITOR.instances.mail_message.destroy(false);
         // --------------------------------------


        // ATTEMPT 4:   
        for(name in CKEDITOR.instances)
        {
            CKEDITOR.instances[name].destroy()
        }
        // ---------------------------------------

        console.log('destroyed');
    } 
// RECREATE: (but code errors out before this, with above-mentioned error.
CKEDITOR.replace('mail_message', { toolbar: 'basic' }); 

UPDATE I have found that just using the replace method recreates - even without destroying first. i guess this is the solution.

Upvotes: 0

Views: 5912

Answers (2)

George Beier
George Beier

Reputation: 256

I would sure think about passing true as the parameter with the destroy if you intend to reinitialize the editor. I have a grid and I click on each row to pull up the information in a jquery modal dialog. Unless I pass "true" as the parameter, I find that setData() doesn't work and, even though I am setting the data and I did destroy the previous instance, the old data appears in the editor. So use:

var editor = CKEDITOR.instances['txt_LH_Update_Body']; if (editor) { editor.destroy(true); }

Upvotes: 0

Abhi
Abhi

Reputation: 112

I have faced same problem when I want to reload the CKeditor, and this code solves my problem. To destroy the old instance use,

if(CKEDITOR.instances.editor1)
CKEDITOR.instances.editor1.destroy();

we will check if the instance is already created or not. and To re-load the CKeditor, we will recreate the CKE object using

 CKEDITOR.replace();

Upvotes: 2

Related Questions