Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't post twice from the same AJAX TinyMCE textarea

Tags:

I've a list of elements editable via a simple AJAX/jQuery edit button, which works great. But when I try editing the same field a second time it doesn't want to play ball.

  1. EDIT - AJAX returns a tinyMCE textarea containing content from MySQL
  2. SAVE - AJAX posts tinyMCE contents to MySQL and displays posted content
  3. EDIT (again) - Returns the same tinyMCE textarea and content as usual
  4. SAVE (again) - The second time save is attempted, returns error: g.win.document is null

Code snippets

var content = tinyMCE.get('content').getContent(); //get tinyMCE content
$("#edititem").load("editItem.php", {content: content}); //jQuery post

Solution - this is how I got it working:

EDIT - when editing, add the tinyMCE control to the textarea

tinyMCE.execCommand("mceAddControl",true,'content');

SAVE - when saving, remove the control for next time

tinyMCE.execCommand('mceRemoveControl',false,'content');
like image 937
Peter Craig Avatar asked Mar 31 '09 00:03

Peter Craig


1 Answers

Just thought I'd add a workaround solution that works in combination with the solution above:

setTimeout(function() {tinyMCE.execCommand("mceAddControl", true, "content");}, 5);

For whatever reason, I'm not sure if it's a timing issue with the DOM manipulation or something else, but a tiny delay makes life better. However the setTimeout() did NOT work in combination with using a jQuery .each() method, such as:

$("textarea").each(function(index) {
    tinyMCE.execCommand("mceAddControl", false, $(this).attr("id"));
});

That must be a whole different timing issue.

Anyhoo, thought I'd share these findings as I'm sure others and even possibly me again will find this posting helpful.

like image 121
gurun8 Avatar answered Oct 02 '22 11:10

gurun8