ckeditor5

How to updateElement() in CKEditor 5?


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.


Solution

  • 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