Ckeditor update textarea
Solution 1
have you figured it out?
I'm using CKEditor version 3.6.1 with jQuery form submit handler. On submit the textarea is empty, which to me is not correct. However there is an easy workaround which you can use, presuming all your CKEditor textareas have the css class ckeditor.
$('textarea.ckeditor').each(function () {
var $textarea = $(this);
$textarea.val(CKEDITOR.instances[$textarea.attr('name')].getData());
});
Execute the above before you do your submit handling ie. form validation.
Solution 2
Before submit do:
for(var instanceName in CKEDITOR.instances)
CKEDITOR.instances[instanceName].updateElement();
Solution 3
Combination of all of the above answers into one.
Create a new custom.js file and add this:
CKEDITOR.on('instanceReady', function(){
$.each( CKEDITOR.instances, function(instance) {
CKEDITOR.instances[instance].on("instanceReady", function() {
this.document.on("keyup", CK_jQ);
this.document.on("paste", CK_jQ);
this.document.on("keypress", CK_jQ);
this.document.on("blur", CK_jQ);
this.document.on("change", CK_jQ);
});
});
});
function CK_jQ() {
for ( var instance in CKEDITOR.instances ) { CKEDITOR.instances[instance].updateElement(); }
}
You don't have to worry about the name of the textarea, just add a class ckeditor
in the textarea, the above and you are done.
Solution 4
Thanks @JohnDel for the info, and i use onchange to make it update every change.
CKEDITOR.on('instanceReady', function(){
$.each( CKEDITOR.instances, function(instance) {
CKEDITOR.instances[instance].on("change", function(e) {
for ( instance in CKEDITOR.instances )
CKEDITOR.instances[instance].updateElement();
});
});
});
Solution 5
ADD Function JavaScript for Update
function CKupdate() {
for (instance in CKEDITOR.instances)
CKEDITOR.instances[instance].updateElement();
}
It's work. Cool
heldopslippers
Updated on July 09, 2022Comments
-
heldopslippers almost 2 years
I am trying to get the ckeditor working. Obviously it doesn't make use of the textarea so on submit the form doesn't submit the text in the editor. Beceause I make use of polymorphic associations etc. I can't make a onsubmit function to get the value of the textarea (when the form is submitted) .
So I found this question: Using jQuery to grab the content from CKEditor's iframe
with some very good answers. The answers posted there keep the textarea up to date. That is very nice and just what I need! Unfortunately I can't get it to work. Does somebody know why (for example) this doesn't work?
I have a textarea (rails but it just translates to a normal textarea):
<%= f.text_area :body, :id => 'ckeditor', :rows => 3 %>
And the following js:
if(CKEDITOR.instances.ckeditor ) { CKEDITOR.remove(CKEDITOR.instances.ckeditor); } CKEDITOR.replace( 'ckeditor', { skin : 'kama', toolbar :[['Styles', 'Format', '-', 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', 'Link']]}); CKEDITOR.instances["ckeditor"].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.ckeditor.updateElement(); }
I get the following "error" in my firebug.
missing ) after argument list [Break on this error] function CK_jQ()\n
-
Eduard7 over 12 yearsSame solution for prototype:
$$('textarea._cke').each( function(T) { T.update( CKEDITOR.instances[T.id].getData() ); } );
-
Bajrang over 12 years@TJ- I am new in Jquery, I dont know how it works ? Could you please write a line of code that alerts the value of ckeditor ? i am trying with :-
alert($textarea.val(CKEDITOR.instances[$textarea.attr('name')].getData()));
. Please... -
T. Junghans over 12 years@J.J. Before you alert assign the jquery object of your textarea to $textarea.
var $textarea = $('textarea.mytextarea');
Then usealert(CKEDITOR.instances[$textarea.attr('name')].getData())
. Do this after the ckeditor has been applied. -
NightMICU about 12 yearsThanks, saved me a lot of time
-
Alex Angelico about 12 yearsI think you have to use $textarea.html() instead of .val()
-
Ben Amada about 12 yearsgetData() and val() work perfectly fine, but see answer by sldev where he uses CKEDITOR's updateElement().
-
Joel Peltonen over 11 yearsBut the OP already calls updateElement all the time and it still didn't work for OP, that's possibly why this isn't the answer here.
-
xtds over 11 years@Nenotlep: the accepted answer is basically the same but is a workaround for my answer since CKEDITOR already provides that with updateElement
-
Joel Peltonen over 11 years@sldev: I agree that your solution is better, I was trying to point out that he already called updateElement() in his solution, although in a not so logical as your place before submit :)
-
András Szepesházi almost 11 yearsWorks great. What beats me is why CKEditor's default save action does not work this way.
-
Mhmd almost 11 yearsafter loading editor by replace, you may add this line >>> CKEDITOR.instances.textAreaClientId.on('blur', function(){CKEDITOR.instances.textAreaClientId.updateElement();});
-
Mark almost 11 yearsI had to use: $('textarea.ckeditor').each(function () { var $textarea = $(this); $textarea.val(CKEDITOR.instances[$textarea.attr('id')].getData()); }); The instance was named by the fields ID not name. But it seems a more supported solution as mentioned above by Ben Amada is: for ( instance in CKEDITOR.instances ) CKEDITOR.instances[instance].updateElement();
-
Nigel Angel almost 11 yearsJust in case anyone reading this needs to do it for FCKeditor, the cases are different:
$(this).val(FCKeditorAPI.Instances[this.id].GetData());
-
t.mikael.d over 9 yearsI wish CKEDITOR did this by default, i can't really see any reason not to?
-
flu over 9 years
instanceReady
is triggered for every editor instance that you have. So if you have multiple instances this will bind theonChange
event multiple times doing a lot of unnecessary work. Instead of the$.each(...)
you should use make use of the event parameter that is provided to theinstanceReady
callback and take itseditor
attribute which is the editor instance that has just called itsinstanceReady
event. -
JDandChips about 9 yearsThis is quite useful if you want to Encode the HTML before it goes into the textarea
-
AllThisOnAnACER almost 6 yearsThis one also works on multiple textareas, loaded by ajax and using CKEDITOR.replace('item', language).
-
zanderwar almost 5 yearsDon't forget
if (CKEDITOR.instances.hasOwnProperty(instanceName)) {