19

I have a form in which I have CKEditor replacing my <textarea>s (multiple). I want to remove all CKEditor instances from the page before submitting the form. How can I accomplish this?

I've looked at Remove CKEdit Instance but it didn't help me at all.

NOTE: All my CKEditors have a class "ckedit"

Community
  • 1
  • 1
erikvimz
  • 5,256
  • 6
  • 44
  • 60
  • Short question, why would you need to do this? – Peon May 19 '15 at 13:21
  • 1
    It's been 3 years since I posted this, I no longer remember what I needed it for. There must have been a reason... – erikvimz May 19 '15 at 18:15
  • Yeah, I came across a solution like this, but have no idea why it's implemented, because it works fine without destroying it first – Peon May 20 '15 at 07:57

3 Answers3

59

This will destroy all CKEDITOR instances on a page:

for(name in CKEDITOR.instances)
{
    CKEDITOR.instances[name].destroy(true);
}
AlfonsoML
  • 12,634
  • 2
  • 46
  • 53
  • 1
    for future people searching. This works great but when I tried to reload I was getting an error saying i.contentWindow was null. After googling I found by passing true to destroy that error will go away – Matt Bodily Feb 03 '14 at 21:00
  • 1
    giving error: TypeError: a is null ....getDocumentElement().clearCustomData();a.clearCustomData();CKEDITOR.tools.remove... – Jitendra Pancholi Apr 14 '15 at 12:06
  • In case of errors try this: for(var a in CKEDITOR.instances){CKEDITOR.remove(CKEDITOR.instances[a]);} – Ciri Aug 21 '15 at 08:29
  • @Serban That's totally wrong. You're only removing it from the list of editor instances, but the whole object will remain in memory and you'll end up with a huge memory leak. – AlfonsoML Aug 21 '15 at 08:47
  • @AlfonsoML unfortunately, nothing else seems to work for me. Your code gives the following error: Uncaught TypeError: Cannot read property 'clearCustomData' of null. When I try to destroy a single editor from the list of instances (using CKEDITOR.instances['inputProjectTarget8391'].destroy(); ) I get this: Uncaught TypeError: Cannot read property 'document' of undefined. – Ciri Aug 21 '15 at 09:41
  • But your code isn't really removing the editor, it's removing it only from a list, but it doesn't remove its elements from the page,the hooked events, etc... your code is only giving a false ilusion and in the long term you'll get some unexpected error that no one else can reproduce because you're ab-using the CKEditor API – AlfonsoML Aug 21 '15 at 14:54
0

you can make use of .remove() of jquery, before submitting.

Piyush Sardana
  • 1,748
  • 4
  • 19
  • 33
  • But won't this remove the entire – erikvimz Jul 27 '12 at 12:23
  • those ckeditor instances, do they any Ids?? like a commin pattern of Ids? ck1, ck2..something like this; – Piyush Sardana Jul 27 '12 at 12:25
  • No unfortunately they do not. I already thought of looping though each element but I do not have time to rewrite that code at the moment. I only have a class 'ckedit' nothing else :( – erikvimz Jul 27 '12 at 12:27
  • okay these instance, do they have any value like null or something..if they have some value then you can loop through all you instances and get the value of every instance using this .val() method of jquer and if value is something then you can remove those instances??? – Piyush Sardana Jul 27 '12 at 12:30
  • There is no repeating value nor attribute I can loop through. Is it possible to loop thought each element with jquery.each and use $(this) to remove the instance somehow? – erikvimz Jul 27 '12 at 12:33
  • yes it is possible but if you want to remove instances then you should have some condition inside each(). each() can interate through all specified elements...check this fiddle http://jsfiddle.net/EjbLx/6/ – Piyush Sardana Jul 27 '12 at 12:36
  • OK I've decided to use IDs, gonna rewrite everything, I tried CKEDITOR.instances.editor1.destroy(); and it works perfectly, does anyone know, can I use that function to somehow loop thought 50 ids and remove them if they exist. I don't wan't to write destory() function for 50 times -.- – erikvimz Jul 27 '12 at 13:03
-15

Have you tried just...

delete CKEDITOR;

I had a similar situation and this worked for me. Just make sure you re-create it the next time you need to use it. Otherwise try keeping an array of id's of all the instances you create and just loop through that array and destroy them all.