CKEditor: restore carriage position after calling editor # setData

I have a CKEditor instance where I want to manipulate the contents and restore the caret position to where it was later. The problem is that when you call setData it resets the carriage to the beginning of the editor. This is understandable if you change all the content, but I make small changes to the data.

 editor.on('change', function () { var data = editor.getData(); // manipulate `data` var manipulatedData = data; editor.setData(manipulatedData); }); 
+7
javascript ckeditor rich-text-editor
source share
2 answers

I found a simple solution for your request. instead of adding setdata. you can use inserthtml

 editor.insertHtml(manipulatedData). 

Will hold the cursor at the end position after inserting data

+1
source share

A simple solution to setData to, '' and then use insertHtml with your content. setData is asyncronius, so you should use the callback function. This is the code that works:

 oEditor.setData('', {callback: function() { oEditor.insertHtml(YOUR_HTML); } }); 
0
source share

All Articles