27Feb08

jquery/AJAX/TinyMCE

Here’s an issue I came across today: I’m updating an article in my CMS, using jquery.ajax, and everything works well, until I notice that the TinyMCE-textarea’s contents isn’t updated.

My workaround/hack, in case someone runs into the same problem:

Example JS-function:

function send_form() {
$.ajax({
type: 'POST',
url: 'foo.php',
data: $('form').serialize() + '&article=' + tinyMCE.getContent('article'),
success: function(){
// Show success-message, reload article-list etc.
},
error: function() {
// // Show error-message
}
});
}

The JS is a stripped down un-tested version of my function, but it shows the workaround: adding a POST-variable at the end of the request, after serializing the form content, getting the textarea’s current content with tinyMCE.getContent:

data: $('form').serialize() + '&article=' + tinyMCE.getContent('article'),

This means that “article” is sent twice, but in my tests the last one is used. I’ll look into how to overwrite the initial “article”, but at least this seems to work.

2 Responses to “jquery/AJAX/TinyMCE”


  1. 1 Jonathan Cummins Posted October 25th, 2010 - 15:14

    If you use tinyMCE.triggerSave(true) before you call the serialize, your first value for the textarea in the post data will be correct and you won’t need to append on to the end

  2. 2 Eirik Posted October 27th, 2010 - 21:17

    Ah, thanks for the tip!

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.




Archives

Categories

Recent Comments