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

 

romain Paris
May 16, 2008

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.
 

Mark L.
June 12, 2008

"tinyMCE.triggerSave();" was all it took. I just wish I read your entry 4 hours earlier :) would have saved the headache.
 

guga nereu
August 26, 2008

ghjghjghj
 

Paul S Wellington, NZ
September 02, 2008

Thanks bro! I was totally bamboozled as to why it wasn't working before finding your post.
 

Karl UK
September 26, 2008

Thanks, saved me after a few hours of head scratching!
 

Scott Long Beach, CA
October 22, 2008

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!
 
write a message
Name


City


Email (won't be displayed)




Antispam: enter the current year (2008)