Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly destroy CKEditor instance?

I am running CKeditor 3.4 on a pretty simple page. I am having a problem (sometimes) where when I call document.main_form.submit(), it will not send along the contents of the textarea. After some reading, it sounds like CKeditor is not destroying properly. I tried to manually destroy it before I save the form, but wasn't able to call it. The weird thing is, it works sometimes, but not others. I'm on Chrome, so that may be screwing with things, but the same thing happens in Firefox.

How can I properly destroy the CKeditor so that it always sends the textarea data in POST. Thanks!

like image 629
Shane Reustle Avatar asked Aug 31 '10 21:08

Shane Reustle


People also ask

How do I get rid of Ckeditor?

When destroy() runs, CKEDITOR. remove(hEd); is being called. Multiple clicks to create() produce multiple instances of CKEditor on screen, but their instances no longer appear in CKEDITOR.

How do I reinitialize Ckeditor?

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

What is Ckeditor replace?

The CKEditor 4 Find and Replace feature allows for finding and replacing any text in the editor easily. It helps the user find words, word parts or phrases matching the case of the searched text, which is especially helpful in lengthy documents and one that may utilize certain words in different contexts.


3 Answers

I had this problem. What a pain.

To properly destroy the editor instance, try

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

From the documentation here

I solved the missing content issue by assigning the contents of the editor to a hidden field prior to postback. I'm using ASP.Net, but it should work universally.

in the client-side click handler of the submit button, call

if (CKEDITOR.instances.myInstanceName)
    document.getElementById('hiddenField').value = CKEDITOR.instances.getData();
like image 92
Laramie Avatar answered Sep 30 '22 14:09

Laramie


I once used angularjs ui-router with one CKEDITOR instance for each sub-view. I used the following solution to clear the instances every time I load the ui-view

for(name in CKEDITOR.instances)
{
    CKEDITOR.instances[name].destroy()
}
like image 27
Bernie GGGG Avatar answered Sep 30 '22 13:09

Bernie GGGG


In my situation

CKEDITOR.instances.myInstanceName.destroy();

didn't help, because I'd opened CKEditor in jquery dialog on double click on some item. When I closed editor and then opened them again, my code crashed.
Solution was using

CKEDITOR.instances.myInstanceName.destroy(false);

which updated DOM element (link to documentation).

like image 36
Tomasz Dzięcielewski Avatar answered Sep 30 '22 14:09

Tomasz Dzięcielewski