Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  189] [ 2]  / answers: 1 / hits: 21625  / 11 Years ago, thu, august 29, 2013, 12:00:00

I already searched a lot but by google-fu'ing don't get me any results :(



I have an already initialized tinyMCE editor which initialization process I cannot control, so code like the following don't work at all:



tinyMCE.init({
...
setup : function(ed) {
ed.onChange.add(function(ed, l) {
console.debug('Editor contents was modified. Contents: ' + l.content);
});
}
});


Code like the following doesn't work either since the jQuery tinymce plugin isn't defined:



$('textarea').tinymce({
setup: function(ed) {
ed.onClick.add(function(ed, e) {
alert('Editor was clicked: ' + e.target.nodeName);
});
}
});


I mean, it has to be using the tinymce.something syntax.



How can I bind a callback function to whatever tinyMCE event after tinyMCE is already initialized?


More From » events

 Answers
18

After hacking into the tinymce object with console.log(), I found a working solution:



setTimeout(function () {
for (var i = 0; i < tinymce.editors.length; i++) {
tinymce.editors[i].onChange.add(function (ed, e) {
// Update HTML view textarea (that is the one used to send the data to server).
ed.save();
});
}
}, 1000);


Inside that callback function one can set the whatever event binding one wants.



The setTimeout call is to overcome the racing condition of tinymce and jQuery, since when the call to tinymce.editors[i].onChange.add() is made tinymce wasn't initialized yet.


[#76029] Wednesday, August 28, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nestorjarettg

Total Points: 451
Total Questions: 108
Total Answers: 108

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;