How change the CKEditor text using jQuery?

user2297392 picture user2297392 · Feb 28, 2014 · Viewed 10.8k times · Source

I have a textarea with CKEditor (bbCode Plugin).

<textarea id="editor1" name="conteudo" class="form-control" rows="3" required></textarea>

This is my CKEditor instance:

$( document ).ready( function() {
    $( 'textarea#editor1' ).ckeditor();
} );

I'm making a JSON request that takes a value and I want this value to be modified in this textarea, I tried with jQuery but not worked ! Below is my attempt:

video_id = "lLi1Lx2xTKI";

$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){
    description = data.data.description;
    // Attempt here
    $("#editor1").html(description);
});

UPDATE

I tried using '.val()' and not worked!

Answer

Praveen picture Praveen · Feb 28, 2014

You can't simply add text to the CKEDITOR via jQuery, instead go with api given by CKEDITOR

CKEDITOR.instances.editor1.setData(data.data.description); 

Here your code looks like

$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){   
    CKEDITOR.instances.editor1.setData(data.data.description); 
});

Fiddle