TinyMCE, jQuery and Ajax forms

TinyMCE Init

Replace textareas with CSS class “wysiwyg” being by TinyMCE editors.

// warning: has to be outside of jQuery's "document ready" block.
 tinyMCE.init({
	mode : "textareas",
	theme : "simple",
	editor_selector : "wysiwyg"
});

TinyMCE Form Submission

  • When a textarea is replaced by TinyMCE, it's actually hidden and TinyMCE editor (an iframe) is displayed instead.
  • However, it's this textarea's contents which is sent when the form is submitted. Consequently its contents has to be updated before the form submission.
  • For a standard form submission , it's handled by TinyMCE . For an Ajax form submission, you have to do it manually, by calling (before the form is submitted):
tinyMCE.triggerSave();

TinyMCE and jQuery Form plugin

With jQuery Form plugin, here's how to update the textareas before the form fields are serialized and sent via Ajax:

$('form').bind('form-pre-serialize', function(e) {
    tinyMCE.triggerSave();
});

Errors

If you get this error with tinyMCE.triggerSave():

t.win.document has no properties

You're dynamically adding/removing textareas without dealing properly with their associated TinyMCE editors:

TinyMCE Init

tinyMCE.init({
    mode : "none",
    theme : "simple"
});

Replace a textarea by a TinyMCE editor

// replace textareaId by the id of your textarea
tinyMCE.execCommand('mceAddControl', false, 'textareaId');

Remove a TinyMCE editor

// replace textareaId by the id of your textarea
tinyMCE.execCommand('mceRemoveControl', false, 'textareaId');

Reference

 

Feedback

Even thought this really seems easy, I believe there's stuff missing. I'm trying to make jquery and TinyMCE works together in ajax mode (silent submit and reload), but I really can't get anything working... some help would be appreciated.
romain
May 16, 2008
#1
"tinyMCE.triggerSave();" was all it took. I just wish I read your entry 4 hours earlier :) would have saved the headache.
Mark L.
June 12, 2008
#2
ghjghjghj
guga
August 26, 2008
#3
Thanks bro! I was totally bamboozled as to why it wasn't working before finding your post.
Paul S
September 2, 2008
#4
Thanks, saved me after a few hours of head scratching!
Karl
September 26, 2008
#5
This may be a dumb question, but where, in the Drupal code, do you put the "tinyMCE.triggerSave();" line? And would you just place this: "tinyMCE.init({ mode : "textareas", theme : "simple", editor_selector : "wysiwyg" });" in the <head> tag of page.tpl.php? Thanks! PS. Basically I am just not getting what the step my step is to implement this code. Thanks again!
Scott
October 22, 2008
#6
Thank you very much. I had a nasty but where TinyMCE and jQuery were fighting which was also disabling or breaking Firebug. The TinyMCE wiki does not clearly address this (that I can see). Changing to running init with mode "none" and then calling mceAddControl solved my problem perfectly.
Jason Smith
December 14, 2008
#7
thanks! great tip!
cwd
May 2, 2009
#8
Where does this go ? $('form').bind('form-pre-serialize', function(e) { tinyMCE.triggerSave(); });

June 4, 2009
#9
Thanks a lot! What a nightmare!
Jon
June 5, 2009
#10
Thanks, you saved my day. I couldn't find anywhere else why jquery was fighting with tinymce!
Andrea
June 10, 2009
#11
Doh! If I only had googled for "tinymce jquery ajax form" earlier! Great post, mate!
TomM
June 18, 2009
#12
thanks so much - you saved me a ton of time trying to figure that one out!
Dan Pickett
June 22, 2009
#13
Thanks!
marss
July 28, 2009
#14
Thank you very much... I Love YOU!!!
darcon3371
August 24, 2009
#15
Really easy and great solution, saved me what would be hours of work. Thanks for the answer!
Gingah
August 25, 2009
#16
You safe my life! Thanks God.
aifarfa
October 1, 2009
#17
This is exactly what I needed. Awesome
maxleb
October 13, 2009
#18
Brilliant. Thank you
Dave
October 24, 2009
#19
You sure saved me a lot of time! Thanks for this very helpful post.
Kathryn
October 30, 2009
#20
Thanks!!!! Wish I read your page a few hours earlier ;-)!
Jordy
November 11, 2009
#21
Nice post! Thx!
Jan
January 13, 2010
#22
LifeSaver. Tks.
Mauricio Wolff
February 5, 2010
#23
Great!!! thanks
RoloTomasi
February 10, 2010
#24
hi, i got two textareas in my form and i would like to keep the second one as a simple textarea. how can i target a specific textarea to be replaced by tinyMCE ?
thanks anyway
imed
February 11, 2010
#25
ok, i think i got the solution :
init tinyMCE with : mode : "none",
then add tinyMCE control to the specific textarea by passing its ID : tinyMCE.execCommand('mceAddControl', false, 'idTargetedTextarea');
imed
February 11, 2010
#26
it was just in front of me (shame!!)
thx
imed
February 11, 2010
#27
THX!

March 1, 2010
#28
Obrigado !!!
Ou melhor ...Thanks !!! ;)
Daniel Medeiros
March 3, 2010
#29
w

March 18, 2010
#30
I don't get this solution to work. :(

Please can you post an example...?
Shaaa
May 7, 2010
#31
Fantastic. Thank you - much appreciated.
Damian
June 11, 2010
#32
Thanks.
Nice post :)
quangpd
July 3, 2010
#33
Cool. The elegant solution he was seeking. Thanks!
EDB
July 15, 2010
#34
good job, thanks!
marcus
July 25, 2010
#35