Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the contents of ckeditor on change?

I have the following code for detecting changes on the ckeditor surface.

CKEDITOR.instances['editor1'].on("instanceReady", function(){                    
this.document.on("keyup", function(){
console.log("sth changed");
});
});

I would like to get the contents of editor on change for counting the words in it. How can I reach it using CKEDITOR.instances?

like image 637
Rápli András Avatar asked Oct 15 '25 13:10

Rápli András


2 Answers

The only correct way to retrieve contents of the editor is:

CKEDITOR.instances.yourInstanceName.getData();

You can also try this.on( 'key', function() { ... } ); because editor fires key events.

Additionally I recommend you the ticket regarding onchange event and monitoring changes in the editor.

And the last but not least: The wordcount plugin which has already been implemented and does your job ;)

like image 178
oleq Avatar answered Oct 17 '25 02:10

oleq


For version 5 you use editor.model.document.on('change:data', ...).

So much like:

let someContent = '';

ClassicEditor.create(document.querySelector('#editor')).then(editor => {
  editor.model.document.on('change:data', () => {
    someContent = editor.getData()
  })
}).catch(err => {
  console.log('error loading ckeditor', err);
})

Read the docs: https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/basic-api.html#listening-to-changes

like image 22
Lawrence Cherone Avatar answered Oct 17 '25 03:10

Lawrence Cherone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!