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?
Although this post is old now but I think other people will need this answer. I had the same problem and to fix it I did this:
Based on http://www.tinymce.com/wiki.php/Configuration3x:init_instance_callback
In TinyMCE 4, onChange doesn't exist. You have to use:
Here is my solution for binding events to one or multiple textareas at any time, given this code is appended after including the tinymce javascript file into your page. (In other words, the only thing required for this to work, is the existence of the 'tinymce' variable)
Note that the 'change' event is not bound to always trigger instantly after something changed. According to the documentation, it “gets fired when changes are made inside the editor that cause an undo level to be added.” Which in my experience, does not always happen when you expect it to happen.
One way to overcome this is by binding multiple events, such as 'input change', however, there will be some overlapping, which should then be filtered.
After hacking into the tinymce object with console.log(), I found a working solution:
Inside that callback function one can set the whatever event binding one wants.
The
setTimeout
call is to overcome the racing condition oftinymce
andjQuery
, since when the call totinymce.editors[i].onChange.add()
is made tinymce wasn't initialized yet.EDIT: oops - I thought this was another question I was looking at that was specific to WordPress + TinyMCE, guess not. Though I'll leave the answer here as it may be helpful to others.
The proper way to do this would be to append to the tinyMCE init with the WordPress filter. For example:
PHP (in functions.php or other location that is run on edit page load):
JavaScript (/js/admin/admin.js)
This is tested and working in WordPress 3.9 (though I am simply getting the console output:
But that's due to tinyMCE deprecating code you're trying to use. This method does still work to modify to any of the tinyMCE init options - I'm currently using it for
init_instance_callback
and it's working great.-Thomas