jQuery and TinyMCE: textarea value doesn't submit

JqueryTinymce

Jquery Problem Overview


I am using jQuery and TinyMCE to submit a form, but there is a problem in serialization in that the Textarea value doesn't post.

Here is the code:

<form id="myForm" method="post" action="post.php">
    <textarea name="question_text" id="question_text" style="width:543px;height:250px;"></textarea>
</form>

language: lang-js

$('#myForm').submit(function() {
    $.ajax({
        type: 'POST',
        url: $(this).attr('action'),
        data: $(this).serialize(),
        success: function(data) {
            $('#result').fadeIn('slow');
            $('#result').html(data);
            $('.loading').hide();
        }
    })
    return false;
});

tinyMCE.init({
    // General options
    mode : "textareas",
    theme : "advanced",

    // Theme options
    theme_advanced_buttons1 : "bold,italic,underline,separator,image,separator,justifyleft,justifycenter,justifyright,jformatselect,fontselect,fontsizeselect,justifyfull,bullist,numlist,undo,redo,styleprops,cite,link,unlink,media,advhr,code,preview",
    theme_advanced_buttons2 : "",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resize_horizontal : false,
    theme_advanced_resizing : true,
    extended_valid_elements :"a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
});

Can you explain to me what I should change, and why, in order to get the value in the text area posted?

Jquery Solutions


Solution 1 - Jquery

Before submitting the form, call tinyMCE.triggerSave();

Solution 2 - Jquery

You can configure TinyMCE as follows to keep the values of hidden textareas in sync as changes are made via TinyMCE editors:

tinymce.init({
    selector: "textarea",
    setup: function (editor) {
        editor.on('change', function () {
            editor.save();
        });
    }
});

The textarea elements will be kept up to date automatically and you won't need any extra steps before serializing forms etc.

This has been tested on TinyMCE 4.0

Demo running at: http://jsfiddle.net/9euk9/49/

Update: The code above has been updated based on DOOManiac's comment

Solution 3 - Jquery

From TinyMCE, jQuery and Ajax forms: > ## 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();

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

Solution 4 - Jquery

That's because it's not a textarea any longer. It's replaced with an iframe (and whatnot), and the serialize function only gets data from form fields.

Add a hidden field to the form:

<input type="hidden" id="question_html" name="question_html" />

Before posting the form, get the data from the editor and put in the hidden field:

$('#question_html').val(tinyMCE.get('question_text').getContent());

(The editor would of course take care of this itself if you posted the form normally, but as you are scraping the form and sending the data yourself without using the form, the onsubmit event on the form is never triggered.)

Solution 5 - Jquery

When you run ajax on your form, you need to tell TinyMCE to update your textarea first:

// TinyMCE will now save the data into textarea
tinyMCE.triggerSave(); 
// now grap the data
var form_data = form.serialize(); 

Solution 6 - Jquery

I used:

var save_and_add = function(){
    tinyMCE.triggerSave();
    $('.new_multi_text_block_item').submit();
};

This is all you need to do.

Solution 7 - Jquery

This will ensure that the content gets save when you lose focus of the textarea

 setup: function (editor) {
                editor.on('change', function () {
                    tinymce.triggerSave();
                });

Solution 8 - Jquery

var text = tinyMCE.activeEditor.getContent();
$('#textareaid').remove();
$('<textarea id="textareaid" name="textareaid">'+text+'</textarea>').insertAfter($('[name=someinput]'));

Solution 9 - Jquery

I had this problem for a while and triggerSave() didn't work, nor did any of the other methods.

So I found a way that worked for me ( I'm adding this here because other people may have already tried triggerSave and etc... ):

tinyMCE.init({
   selector: '.tinymce', // This is my <textarea> class
   setup : function(ed) {
                  ed.on('change', function(e) {
                     // This will print out all your content in the tinyMce box
                     console.log('the content '+ed.getContent());
                     // Your text from the tinyMce box will now be passed to your  text area ... 
                     $(".tinymce").text(ed.getContent()); 
                  });
            }
   ... Your other tinyMce settings ...
});

When you're submitting your form or whatever all you have to do is grab the data from your selector ( In my case: .tinymce ) using $('.tinymce').text().

Solution 10 - Jquery

You can also simply use the jQuery plugin and package for TinyMCE it sorts out these kinds of issues.

Solution 11 - Jquery

@eldar: I had the same issue with 3.6.7 running in 'normal mode'; and neither triggerSave or save() were working.

I changed to the jQuery TinyMCE plugin and without having to do anything else it's working now. I'm assuming somewhere along the line they implemented some kind of auto triggerSave for the jQuery version of TinyMCE.

Solution 12 - Jquery

I just hide() the tinymce and submit form, the changed value of textarea missing. So I added this:

$("textarea[id='id_answer']").change(function(){
    var editor_id = $(this).attr('id');
    var editor = tinymce.get(editor_id);
    editor.setContent($(this).val()).save();
});

It works for me.

Solution 13 - Jquery

tinyMCE.triggerSave(); seems to be the correct answer, as it will sync changes from the iFrame to your textarea.

To add to the other answers though - why do you need this? I had been using tinyMCE for awhile and hadn't encountered issues with form fields not coming through. After some research, it turned out to be their "auto patching" of form element submits, which is on by default - http://www.tinymce.com/wiki.php/Configuration3x:submit_patch

Basically, they redefine submit to call triggerSave beforehand, but only if submit hasn't already been redefined by something else:

if (!n.submit.nodeType && !n.submit.length) {
    t.formElement = n;
    n._mceOldSubmit = n.submit;
    n.submit = function() {
        // Save all instances
        tinymce.triggerSave();
        t.isNotDirty = 1;

        return t.formElement._mceOldSubmit(t.formElement);
    };
}

So, if something else in your code (or another 3rd party library) is messing with submit, their "auto patching" won't work and it becomes necessary to call triggerSave.

EDIT: And actually in the OP's case, submit isn't being called at all. Since it's ajax'd, this is bypassing the "auto patching" described above.

Solution 14 - Jquery

First of all:

  1. You must include tinymce jquery plugin in your page (jquery.tinymce.min.js)

  2. One of the simplest and safest ways is to use getContent and setContent with triggerSave. Example:

     tinyMCE.get('editor_id').setContent(tinyMCE.get('editor_id').getContent()+_newdata);
     tinyMCE.triggerSave();
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAgus PuryantoView Question on Stackoverflow
Solution 1 - JqueryeldarView Answer on Stackoverflow
Solution 2 - JqueryDan MalcolmView Answer on Stackoverflow
Solution 3 - JquerymorganView Answer on Stackoverflow
Solution 4 - JqueryGuffaView Answer on Stackoverflow
Solution 5 - JqueryKris KhairallahView Answer on Stackoverflow
Solution 6 - JqueryBob RobertsView Answer on Stackoverflow
Solution 7 - JqueryCyberNinjaView Answer on Stackoverflow
Solution 8 - JquerySwystView Answer on Stackoverflow
Solution 9 - JqueryJames111View Answer on Stackoverflow
Solution 10 - JquerySpockeView Answer on Stackoverflow
Solution 11 - JquerySupaMonkeyView Answer on Stackoverflow
Solution 12 - JquerygzeroneView Answer on Stackoverflow
Solution 13 - JqueryAndrew TevingtonView Answer on Stackoverflow
Solution 14 - JqueryA.NeamatiView Answer on Stackoverflow