Scott
Scott

Reputation: 2760

ckeditor 5 - How do you get the editor data?

I have the following div:

<div name="content" id="editor">

I have the following script:

<script>
    $(document).ready(function () {
        // CKEDITORs
        var myeditor = ClassicEditor
            .create(document.querySelector('#editor'))
            .then(editor => {
                console.log(editor);
            })
            .catch(error => {
                console.error(error);
            });
    });

    const data = editor.getData();
    const data = myeditor.getData();

</script>

From the documentation, I thought I would be able to do the following:

const data = editor.getData(); //fails with editor.getData is not a function

So added the myeditor var as above and tried this:

myeditor.getData();// also fails with same error.

How do I get data?

Upvotes: 1

Views: 4205

Answers (1)

Naresh Kumar
Naresh Kumar

Reputation: 1736

Usually the creditor data is obtained as

CKEDITOR.instances.editorid.getData();

But in CKEditor 5 there's no single global editor instance like old versions so we have to manually create a instance to hold the data globally to get the data when needed.

let editorinstance;
<script> 
    $(document).ready(function () {
    // CKEDITORs 
    var myeditor = ClassicEditor.create(document.querySelector('#editor'))
    .then
    (editor => { editorinstance =editor;})
    .catch(error => { 
        console.error(error); 
        }); 
}); 

const data = editorinstance.getData(); 
</script>

Upvotes: 4

Related Questions