admin管理员组

文章数量:1202577

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('/'+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!

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!

Share Improve this question edited Feb 28, 2014 at 20:49 asked Feb 28, 2014 at 20:41 user2297392user2297392 5
  • tried $( 'textarea#editor1' ).val(description) ? – Wilmer Commented Feb 28, 2014 at 20:48
  • Yes, @Wilmer. Not worked. – user2297392 Commented Feb 28, 2014 at 20:48
  • 1 No reason to use jQuery... CKEDITOR.instances.editor1.setData("<p>HELLO</p>"); – epascarello Commented Feb 28, 2014 at 20:53
  • You need to include the jquery adapter file if you want to use val() check docs.cksource.com/CKEditor_3.x/Developers_Guide/jQuery_Adapter – Wilmer Commented Feb 28, 2014 at 20:58
  • Here's the jQuery adapter guide for CKEditor 4. – Reinmar Commented Mar 1, 2014 at 11:35
Add a comment  | 

2 Answers 2

Reset to default 21

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

Instead of writing the description directly into the text area, try the CKEditor setData method. You can find a description of it here:

http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-popup

also be sure that your description variable does have a value, I'd use a temporary alert(description); for this but you may be able to do it with a javascript debugger also.

本文标签: javascriptHow change the CKEditor text using jQueryStack Overflow