Add TinyMCE Editor dynamically with different configuration
By Akbar • February 13th, 2008
TinyMCE Editor allows you to have multiple layout configurations (different type of buttons and toolbars) per theme. These custom layout configurations are automatically applied to the text boxes by the depending on the ‘editor_selector’ class of each text boxes.
All works beautifully until you try to add a text control with the correct CSS class dynamically using JavaScript (normal in AJAX calls). When applying the TinyMCE Editor cynically on TEXT boxes, it seems to always apply the last defined setting/configuration. This might be not what you have expected. After a quick digging into the TinyMCE call chain, I was able to add a new method in the TinyMCE source which does the required job. Here is the method:
mceAddControlDynamic : function(control_id, selector_class) {
var bControlAdded = false;
// Check all the stored settings for the match
for (c=0; c<tinyMCE.configs.length; c++) {
var configSettings = tinyMCE.configs[c];
if (configSettings.editor_selector && configSettings.editor_selector == selector_class) {
tinyMCE.settings = configSettings;
tinyMCE.addMCEControl(tinyMCE._getElementById(control_id), control_id);
break;
}
}
}
The method gets the Text Box ID and the editor_class of the settings you want to apply. It will then check all the available configurations and will load the correct one. Seems to do the job for me.
This entry was posted
on Wednesday, February 13th, 2008 at 1:06 pm and is filed under JavaScript.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
GREAT tnx,
On old version (2.x.x) of TinyMCE (plugin compatiblity with the rest of the website).
Works for me..
Hi,
i used the function in tinyMCE 2.xx to.
No i’ve upgraded to 3.2xx.
But i can’t find a way to get function included nor getEditor configurations switched in tinyMCE.
Right, I noticed that too when looking for the upgrade option to the 3+ version.
We are planning to upgrade soon to v3, so I will fix this when I get some free time. If you want to do it on priority, I would suggest that you add a code to the latest version to maintain the config array and then use the above method.
This should be a straight forward task.
Let me know if you are stuck anywhere.
hi,
your method is great for tinyMCE v2, but it looks like configs has been removed from v3+, so i’m itching my back, any other idea?