views:

1960

answers:

3

Hi!

I'm using CKEditor on a textarea and the jQuery validation plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/)

With the jQuery plugin it's possible to mark a field as empty or required. When a field e.g. "product name" is empty the field will be marked as invalid when submitting the form.

But, while typing in a "product name", the field will be automatically marked as valid while typing. That's great, but I want to have the same with my CKEditor-enabled textarea. Everything works great with a plain textarea (when typing text, the field becomes valid), but this doesn't work anymore after adding CKEditor to the textarea.

From then on, you have to click the submit button before the CKEditor textarea gets re-validated.

Any ideas on how I can make this work?

Thanks a lot for your help, stackoverflow has been a great help the last few days!

UPDATE:

I tried the solution from the thread you gave me but it doesn't work: http://stackoverflow.com/questions/924145/using-jquery-to-grab-the-content-from-ckeditors-iframe

I have:

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

but that keeps giving me: "CKEDITOR.instances.page_content.document is undefined"

The id of my textare is "page_content"

This works fine after clicking on the button, but as you see I need to trigger the keydown event somehow

$("#btnOk").click(function(event) {
      CKEDITOR.instances.page_content.updateElement(); //works fine
});

UPDATE 2:

This code is correct and it gets the data from CKEDITOR into my textarea, but still, nothing is happening with my validation plugin, it's not "responding as I type" in the CKEDitor while it should react and say that the field is OK when I type in some text

CKEDITOR.instances["page_content"].on("instanceReady", function()
     {
       //set keyup event
       this.document.on("keyup", updateTextArea);
        //and paste event
       this.document.on("paste", updateTextArea);

     });

     function updateTextArea()
     {
      CKEDITOR.tools.setTimeout( function()
           { 
               $("#page_content").val(CKEDITOR.instances.page_content.getData());
           }, 0);  
     }
+1  A: 

You would have to tweak the CKEditor textarea to write back the HTML when it loses focus, instead of (I assume this is what it does by default) when the form is submitted.

Somebody had a similar question a few days ago and seems to have been successful, check it out.

Pekka
I edited my question, the thread you gave me is indead what I need, but I can't get it to work. Check out my edited question above. Thanks for helping out !
Jorre
I am in exactly the same situation in a different context (accessing a CKEditor instance to set its class body) and had similar problems. I asked a question and got an answer that seems to solve it. I haven't had the time to look at it yet but maybe it helps you: http://stackoverflow.com/questions/1844569/ckeditor-class-or-id-for-editor-body
Pekka
that's indeed a step in the right direction, but I cannot get a click of keydown event to trigger correctly... I got: CKEDITOR.document.on('click', function(event)but that triggers an event when clicking in my normal document body, not inside the CKEDitor?
Jorre
Check out the highest rated answer to my question I linked in the comment. Using nemisj's method, I can set the class of the CKEDitor's body. You should be able to add listeners to it that way.
Pekka
I'm getting closer : this gets the correct data, but still, the jQuery validation plugin isn't responding to this code. CKEDITOR.instances["page_content"].on("instanceReady", function() { //set keyup event this.document.on("keyup", updateTextArea); //and paste event this.document.on("paste", updateTextArea); }); function updateTextArea() { CKEDITOR.tools.setTimeout( function() { $("#page_content").val(CKEDITOR.instances.page_content.getData()); }, 0); }
Jorre
I don't know how the plugins works in detail, so I can't help you there. You may be successful with placing an onchange event on the textarea in your original document.
Pekka
+1  A: 

Ok everybody, I have fixed it thanks to your tips: The jquery validation plugin is reacting to the keyup event in the textarea or text fields, so you need to trigger that event after updating your textarea.

Check this out:

 CKEDITOR.instances["page_content"].on("instanceReady", function()
 {
   //set keyup event
   this.document.on("keyup", updateTextArea);
    //and paste event
   this.document.on("paste", updateTextArea);

 });

 function updateTextArea()
 {
  CKEDITOR.tools.setTimeout( function()
       { 
           $("#page_content").val(CKEDITOR.instances.page_content.getData());
           $("#page_content").trigger('keyup');
       }, 0);  
 }
Jorre
A: 

Thanks for sharing Jorre. Solved my exact problem.

Paul