Categories
jQuery

Get Content from CkEditor Using JQuery

In a recent project I used the CkEditor to add a WYSIWYG editor to the private control panel. It was easy to add the editor to the page, but I struggled to get content from CkEditor using jQuery.

Normally I would use the following code to get content on submit. But that does not work when using CkEditor .

var myContent = jQuery('#my_content').val();

The above code works as long as it is a textarea. Once you add the CkEditor class to the textarea the textarea is replaced with a rich text editor.

#my_content

is the id of the textarea field on the form. For example

< textarea id="my_content" class="ckeditor" >

Now to get the content on submit you have to use the following

var myContent = CKEDITOR.instances.my_content.getData()