27

I have a custom-written CMS that uses CKEditor *(FCKEditor v3) for editing content. I'm also using the jQuery Validation plugin to check all fields for error prior to AJAX-based submission. I'm using the serialize() function to passing the data to the PHP backend.

Problem is, serialize manages to grab all fields correctly, except for the actual content typed in CKEditor. Like every other WYSIWYG editor, this one too overlays an iframe over an existing textbox. And serialize ignores the iframe and looks only into the textbox for content, which, of course, it doesn't find, thus returning a blank content body.

My approach to this is to create a hook onto the onchange event of CKEditor and concurrently update the textbox (CKEDITOR.instances.[textboxname].getData() returns the content) or some other hidden field with any changes made in the editor.

However, since CKEditor is still in it's beta stage and severely lacks documentation, I can't find a suitable API call that'll enable me to do so.

Does anyone have any idea on how to go about this?

3
  • 1
    I've figured out as far as grabbing the content from the iframe: $( '#cke_contents_body iframe' ).contents().find( 'body' ).html()... the closest directly addressable element being a td with the id, 'cke_contents_body'. CKEditor wraps the iframe with this td. May 29, 2009 at 3:49
  • Still to go.. a way to auto update the textbox with the data by hooking onto a change event of CKEditor. Any ideas? Anyone? May 29, 2009 at 3:54
  • 1
    New CKEditor version has resolved this problem
    – Ivan
    Jul 6, 2011 at 18:09

11 Answers 11

36

Another generic solutions to this would be to run the following whenever you try to submit the form

for ( instance in CKEDITOR.instances )
            CKEDITOR.instances[instance].updateElement();

This will force all CKEDITOR instances in the form to update their respective fields

2
  • 1
    @J.J. yes. it is javascript code that should be run on the submit event of the form (or anytime before it). Feb 15, 2012 at 10:16
  • Gaby aka G. Petrioli - Thanks.
    – Bajrang
    Feb 15, 2012 at 13:47
7

I've just released a CKEditor plugin for jQuery which will take care of all this in the background without any extra code: http://www.fyneworks.com/jquery/CKEditor/

2
  • Cool stuff. Thanks. That sure makes it easy. Nov 30, 2009 at 8:11
  • This link is broken as of 2018-09-14 Sep 14, 2018 at 17:49
6

I have also been trying to fix this problem today. I realised that the reason the above code isnt working for me is because the CKEditor instance isnt ready yet when the document property is referenced. So you have to call the "instanceReady" event and within that the document's events can be used, because prior to that it just doesnt exist.

This example might work for you:

CKEDITOR.instances["editor1"].on("instanceReady", function()
{
//set keyup event
this.document.on("keyup", CK_jQ);

 //and paste event
this.document.on("paste", CK_jQ);
});

function CK_jQ()
{

    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
}
1
  • $("#editor1").val(CKEDITOR.instances.editor1.getData()); Working Thanks Sep 16, 2014 at 9:39
3

This should do it...

CKEDITOR.instances["editor1"].document.on('keydown', function(event)
{
    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
});

CKEDITOR.instances["editor1"].document.on('paste', function(event)
{
    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
});

edit: added section to update textbox after pastes, too...

5
  • Thanks for your reply... but it keeps telling me: "CKEDITOR.instances.editor1.document is undefined" !! May 29, 2009 at 9:20
  • BTW, can you please explain why you're using a setTimeout here and not just copying over the content directly upon keystroke? May 29, 2009 at 9:23
  • 2
    replace editor1 by your textboxname. The setTimeout is to make sure we get the contents after the pressed key is added, rather than before.
    – Stobor
    Jun 1, 2009 at 4:03
  • you are refering to keyup?
    – Spons
    May 27, 2013 at 6:58
  • @spons In hindsight, yeah, keyup might have been better.
    – Stobor
    May 28, 2013 at 7:44
2

I had success with this:

console.log(CKEDITOR.instances.editor1.getData());
1

I took a slightly different approach I figured it would be better to use ckeditor's update function and since keyup was being used the timeout wasn't needed

CKEDITOR.instances["editor1"].on("instanceReady", function()
{
//set keyup event
this.document.on("keyup", CK_jQ);

 //and paste event
this.document.on("paste", CK_jQ);
}

function CK_jQ()
{
   CKEDITOR.instances.editor1.updateElement(); 
}
1

Wouldn't it be better to do just this:

CKEDITOR.instances.editor1.on('contentDom', function() {
          CKEDITOR.instances.editor1.document.on('keyup', function(event) {/*your instructions*/});
        });

ref: http://cksource.com/forums/viewtopic.php?f=11&t=18286

1

The contentDom event worked for me and not the instanceReady... I would really like to know what the events ae, but I assume they are proprietary...

var editor = CKEDITOR.replace('editor');

CKEDITOR.instances.editor.on("instanceReady", function(){
    this.on('contentDom', function() {
        this.document.on('keydown', function(event) {
            CKEDITOR.tools.setTimeout( function(){ 
                $(".seldiv").html(CKEDITOR.instances.editor.getData()); 
            }, 1);
        });
    });
    this.on('contentDom', function() {
        this.document.on('paste', function(event) {
            CKEDITOR.tools.setTimeout( function(){ 
                $(".seldiv").html(CKEDITOR.instances.editor.getData()); 
            }, 1);
        });
    });
    edits_clix();
    var td = setTimeout("ebuttons()", 1);
})
1
  • Your answer saved me after close to 4 hours of debugging/rewriting everything. What I'd written was working mostly fine using "instanceReady", but it wasn't working at all on AJAXed in content. Just wanted to say thanks.
    – narx
    Sep 6, 2012 at 21:16
1

CKEDITOR.instances.wc_content1.getData() will return the ckeditor data
CKEDITOR.instances.wc_content1.setData() will set the ckeditor data

0

I think the user was asking about serializing, I was struggling serializing a form to submit and it was giving me a lot of issues.

This is what worked for me:

$(document).ready(function() {
$('#form').submit(function(){
if ( CKEDITOR.instances.editor1.getData() == '' ){
    alert( 'There is no data available' );//an alert just to check if its working
}else{
    var editor_data = CKEDITOR.instances.editor1.getData();
    $("#editor1").val(editor_data); //at this point i give the value to the textarea
    $.ajax({ 
                    //do your ajax here  

                     });

        }
return false;
    });
 });
0

I solved this problem with the current version: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js

After line 55 this.submit( function( event ) { - i added this code:

if (typeof CKEDITOR !== "undefined") {
    for ( instance in CKEDITOR.instances )
        CKEDITOR.instances[instance].updateElement();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.