Jayaraj
Jayaraj

Reputation: 696

Replace selected content in the ckEditor with new content using javascript

I am using CKEditor ver.3.6 in my MVC Application.

My requirement is to update the selected text with new text in the ckEditor. I could find out the method editor.getSelection().getSelectedText(); for getting selected text from the editor. I need to add some tag with the selected text when a toolbar button is pressed and update the selected content using javascript.

For Example :

Content in the ckEditor is

 <span>Edit content in the editor</span>

and I have selected the word “editor” from ckEditor. I have to update the selected word “editor” with “ckEditor” using javascript code.

Please suggest a proper solution.

Upvotes: 9

Views: 7958

Answers (3)

Shlomi Nissan
Shlomi Nissan

Reputation: 123

Both editor.insertText() and editor.insertHtml() should work, but you have to make sure the editor is ready before you attempt to update the text:

var editor = CKEDITOR.replace('editor');

editor.on('instanceReady', function(){
    editor.insertHtml('...');
});

Upvotes: 1

Sunil Raj
Sunil Raj

Reputation: 460

Use this function in the onclick event of a button.

function Replace()
 {
  //after selecting the text in the editor
  //get text to replace;    
  var repStr=$("#repTxt").val();        
  editor.insertHtml(repStr);    
 }

Cheers Sunil Raj

Upvotes: 4

Tim Down
Tim Down

Reputation: 324567

It looks to me from the docs as the following would work (untested):

editor.insertText("ckEditor");

Upvotes: 4

Related Questions