4

I'm trying to update some initialized CKEditors but it doesn't work.

In CKEditor 4 it was:

for(var instanceName in CKEDITOR.instances) 
    CKEDITOR.instances[instanceName].updateElement();

Does not work for CKEditor 5.

Reinmar
  • 21,729
  • 4
  • 67
  • 78

1 Answers1

9

My answer will depend on ClassicEditor but it will also work with the InlineEditor and BalloonEditor.

The ClassicEditor.create() function returns a Promise that will be resolved with the editor instance. There is no longer any global variable that holds created editors. See https://stackoverflow.com/a/46819396/3388924.

If you initialized the editor on a <textarea> element (which should be inside a <form> element), the value of the <textarea> will be updated automatically when the form is submitting. You can see the source code.

Of course, you can update the element manually. See the ClassicEditor.updateElement() method. I also prepared an online demo: https://jsfiddle.net/pomek/k1wp093n/

Keep in mind that editor.updateElement() function in the future release will be renamed to editor.updateSourceElement(). See ckeditor/ckeditor5-core#129

pomek
  • 353
  • 2
  • 8
  • $('.ck-editor__editable')[3].ckeditorInstance.updateSourceElement() seems to work (jquery et al.) – Spikolynn Dec 13 '21 at 14:12
  • Is there no way to trigger `updateElement` this without timeout? I would like to have changes instandly when something happens in the Editor, I could imagin there should be any kind of "editor has changed" event or something, – Radon8472 Apr 26 '23 at 08:43