Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get CKEditor value w/ Jquery

I am referencing the CKEditor JQuery adapter (as well as jquery 1.6 lib)

<script type="text/javascript" src="../ckeditor/ckeditor.js" />  
<script type="text/javascript" src="../ckeditor/adapters/jquery.js" />

And declaring, my CKEditor instance as:

<textarea id="editor1" name="editor1"></textarea>
<script type="text/javascript">
CKEDITOR.replace( 'editor1', {
toolbar : 'Basic',
uiColor : '#0579b3',
resize_enabled: false
}); 
</script>

In Jquery I am doing:

var value = $('textarea.editor1').getData();

If I try to alert the var value, I get undefined.

Is there something wrong with how I am trying to get the textarea value w/ JQuery? I have also tried .val() but no luck.

The alert happens after a button is pressed.

like image 711
H Patel Avatar asked Dec 28 '11 05:12

H Patel


People also ask

How do I get CKEditor content?

If you need to get the actual data from CKEditor 4 at any moment using JavaScript, use the editor. getData() method as described above.

Does CKEditor use jQuery?

CKEditor 4 offers native jQuery integration through its jQuery Adapter (a jQuery plugin basically). It provides deep integration of CKEditor 4 and jQuery that lets you use the native features of jQuery when using CKEditor 4.

How do you call CKEditor in HTML?

To start, create a simple HTML page with a <textarea> element in it. You will then need to do two things: Include the <script> element loading CKEditor 4 in your page. Use the CKEDITOR.


2 Answers

Try:


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

//or
$('#editor1').ckeditor(function( textarea ){
  $(textarea).val();
});

Hope it helps

like image 129
Sudhir Bastakoti Avatar answered Oct 18 '22 19:10

Sudhir Bastakoti


You could integrate a function on JQuery

jQuery.fn.CKEditorValFor = function( element_id ){
  return CKEDITOR.instances[element_id].getData();
}

and passing as a parameter the ckeditor element id

var editor1_value = $().CKEditorValFor('editor1');
like image 28
p1nox Avatar answered Oct 18 '22 19:10

p1nox