Qing Ye
Qing Ye

Reputation: 43

CKEditor - insertText not working after setData

I try to insert something after clear the content by using setData(""), then I insert something to it using insertText method. But the content remains empty.

Relevant code:

CKEDITOR.instances.content.setData("");
CKEDITOR.instances.content.focus();
CKEDITOR.instances.content.insertText("the text I want to insert");

The code above is not working. Anyone could help me? finding another way to clear the content in CKEditor rather than using setData("") or make the insertText method working after setData would both help solve my problem. Many thanks!

Upvotes: 4

Views: 10913

Answers (4)

matias.g.rodriguez
matias.g.rodriguez

Reputation: 849

I had the same problem but installing the jquery adapter as suggested by the accepted solution is not an option for me.

I was able to make it work with the following workaround on the second call:

setTimeout(function(){
  CKEDITOR.instances["myEditor"].setData(newText);
}, 0);

I got the idea from here: https://dev.ckeditor.com/ticket/10663#comment:7

Upvotes: 0

Edward
Edward

Reputation: 4977

I've got a same problem but the point was that setData() method is asynchronous. If you want to run something after setData(), you have to create a callback method.

var fooCallback = function(){
    CKEDITOR.instances.content.focus();
    CKEDITOR.instances.content.insertText("the text I want to insert");
};
CKEDITOR.instances.content.setData("", fooCallback);

Upvotes: 5

Dantte
Dantte

Reputation: 11

I encountered the same issues and found a solution. When "clearing" your editor, do not use an empty string, instead, use CKEDITOR.yourEditor.setData('<span></span>'), or (probably) some other equivalent "valid html" that displays nothing. I have only tried this with the spans for now. subsequent calls to the setData method will not fail. Enjoy.

Upvotes: 1

Sven
Sven

Reputation: 154

Works for me neither. Must be a bug. But with jquery there is a way arround.

$('#content').val('the text I want to insert');

For the jquery adapter have a look here: http://ckeditor.com/blog/CKEditor_for_jQuery

Upvotes: 0

Related Questions