How do I set a value in CKEditor with Javascript?

JavascriptCkeditor

Javascript Problem Overview


I am wondering how I can set a value in CKEditor using Javascript?

I have tried the following, but neither of them work...

document.[form name].[textarea name].value=data;
$('#textareaID').val(data);

However, both these work without the editor applied. Is there a way I can do this with the editor?

Javascript Solutions


Solution 1 - Javascript

Use the CKEditor method setData():

CKEDITOR.instances[**fieldname**].setData(**your data**)

Solution 2 - Javascript

The insertHtml() and insertText() methods will insert data into the editor window, adding to whatever is there already.

However, to replace the entire editor content, use setData().

Solution 3 - Javascript

Use insertHtml() or insertText() method.

Solution 4 - Javascript

Try This

CKEDITOR.instances['textareaId'].setData(value);

Solution 5 - Javascript

I have used the below code and it is working fine as describing->

CKEDITOR.instances.mail_msg.insertText(obj["template"]);

Here-> CKEDITOR ->Your editor Name, mail_msg -> Id of your textarea(to which u bind the ckeditor), obj["template"]->is the value that u want to bind

Solution 6 - Javascript

<textarea id="editor1" name="editor1">This is sample text</textarea>

<div id="trackingDiv" ></div>

<script type="text/javascript">
    CKEDITOR.replace( 'editor1' );

</script>

Let try this..

Update :

To set data :

Create instance First::

var editor = CKEDITOR.instances['editor1'];

Then,

editor.setData('your data');

or

editor.insertHtml('your html data');

or

editor.insertText('your text data');  

And Retrieve data from your editor::

editor.getData();

If change the particular para HTML data in CKEditor.

var html = $(editor.editable.$);
$('#id_of_para',html).html('your html data');

These are the possible ways that I know in CKEditor

Solution 7 - Javascript

As now to day CKEditor 4+ launched we have to use it.ekeditor 4 setData documentation

CKEDITOR.instances['editor1'].setData(value);

Where editor1 is textarea Id.

Old methods such as insertHtml('html data') and insertText('text data') also works fine.

and to get data use

var ckdata =  CKEDITOR.instances['editor1'].getData();
var data = CKEDITOR.instances.editor1.getData();

Ckedtor 4 documentation

Solution 8 - Javascript

Sets the editor data. The data must be provided in the raw format (HTML). CKEDITOR.instances.editor1.setData( 'Put your Data.' ); refer this page

Solution 9 - Javascript

I tried this and worked for me.

success: function (response) {
    document.getElementById('packageItems').value = response.package_items;
                
    ClassicEditor
    .create(document.querySelector('#packageItems'), {
        removePlugins: ['dragdrop']
    })
    .then(function (editor) {
        editor.setData(response.package_items);
    })
    .catch(function (err) {
        console.error(err);
    });
},

Solution 10 - Javascript

Take care to strip out newlines from any string you pass to setData(). Otherwise an exception gets thrown.

Also note that even if you do that, then subsequently get that data again using getData(), CKEditor puts the line breaks back in.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondcp3450View Question on Stackoverflow
Solution 1 - JavascriptefeycView Answer on Stackoverflow
Solution 2 - JavascriptWickView Answer on Stackoverflow
Solution 3 - JavascriptAnpherView Answer on Stackoverflow
Solution 4 - JavascriptSunil kumarView Answer on Stackoverflow
Solution 5 - JavascriptSachinView Answer on Stackoverflow
Solution 6 - JavascriptMari SelvanView Answer on Stackoverflow
Solution 7 - JavascriptDevaView Answer on Stackoverflow
Solution 8 - JavascriptSatya PrakashView Answer on Stackoverflow
Solution 9 - JavascriptDhanushka UdayangaView Answer on Stackoverflow
Solution 10 - JavascriptbcrView Answer on Stackoverflow