How to get value of CKEditor 5?

Latifa Khalid picture Latifa Khalid · Oct 30, 2017 · Viewed 17.3k times · Source

I want to be able to return the value of the CKEditor textarea, and also write my text inside it.

I used CKEditor 5 CDN. First this my code for the textarea it works fine

<script src="https://cdn.ckeditor.com/ckeditor5/1.0.0-alpha.1/classic/ckeditor.js"></script>

<textarea class="inputStyle" id="editor" name="content" placeholder="Write your email.."></textarea>
<script>ClassicEditor.create(document.querySelector('#editor')).catch( error => { console.error( error );  } ); </script>

I used to get the data from the textarea before the CKEditor by:

var text = $('textarea#editor').val();

and set the data by:

$('textarea#editor').html("");

but i'm lost now? i tried many ways... what is the correct way?

Answer

itdoesntwork picture itdoesntwork · Oct 30, 2017

You need to get or save the editor created and then use the getData() function. You can add a .then() on creation to save your editor.

    var myEditor;

    ClassicEditor
        .create( document.querySelector( '#editor' ) )
        .then( editor => {
            console.log( 'Editor was initialized', editor );
            myEditor = editor;
        } )
        .catch( err => {
            console.error( err.stack );
        } );

and then get data using

myEditor.getData();