I am using TinyMCE and I want to set the default font-size to 12.
Here, I assigned a variable to the active TinyMCE editor, but it’s not working…
HTML content:
<textarea id="text_message"></textarea>
JavaScript:
$("#text_message").tinymce({
// Location of TinyMCE script
script_url: '/static/app/a/scripts/tinymce/jscripts/tiny_mce/tiny_mce.js'
});
var fontSize = 12;
tinyMCE.activeEditor.formatter.apply('fontSize',
{
value: fontSize
});
There’s a couple of different event stages you should wait for:
tinyMCE.init()can also be used, depending on the situation.)The first can be accomplished using either:
Or:
Both handle events related to the readiness of the page.
$.ready()triggers when the DOM is available,$.load()in the case ofwindowtriggers when the page content has been loaded (such as inlineimg,script, andlinksources)..ready()fires slightly before the other, but in many cases either will work.Note, below I’ve got
jQuery(function($){...});which is a shortcut to$(document).ready();and helps manage conflicts with other frameworks that use$globally.At the second stage you need to add an initialization event handler to your editor creation request, such as:
In full:
Here is where you can set the display value for
font-size(using the Javascript property offontSize, since Javascript doesn’t allow-in labels such asfont-size). This waits for the editor you’re adding to initialize and say “Hey, I’m here now”. Once you get that notification of the vent firing, you can set the editorbody(whatac.getBody()returns) to befont-size: 18px.What this means is that the
font-sizewill not be returned once you choose the save the editor content; it’s above it in in the editor “body” (literally, theiframe‘sbodyelement), in most cases (unless you choose theplugins : "fullpage"initialization option). So it’s a “default”, but does not get set explicitly within the content itself.If you have Chrome or Firefox with Firebug add-on installed, watch the
consoleon this fiddle:http://jsfiddle.net/userdude/9PuUx/1/
Here is the working code demo and link to a fiddle demo (I use
18pxto easily see the change):http://jsfiddle.net/userdude/9PuUx/
The tinyMCE documentation and API can be a bit confusing and lacking, and I’m not an expert in using it either, so I know there’s perhaps four or five other ways to interpret what’s going on and how to do this. Your mileage may vary. If you have an issue with the way I did this or it does not seem to do what you want or expect, just leave a comment and let me know.