views:

1851

answers:

6

I have an instance of CKEditor on a page. I am trying to give the CKEditor's body a class or ID so it matches some styles I have defined in a stylesheet.

There is a API documentation that should give access to the respective DOM elements, but I can't seem to get it working. All objects I try to query that way turn out undefined.

Does anybody know how to do this, or how to properly address CKEditor's dom elements?

Edit: Thanks folks, nemisj's answer did it for me but for some reason, I don't get to set the "accepted" checkmark in this question.

A: 

Don't know that editor, but as they all work the same way, you probably can't access the DOM elements created by the instance because they are created after the page has finished loading, and the DOM is ready as well. So, any new DOM elements added after that, theorically will not exist.

Still, you can try TinyMCE editor, wich has a "partnership" with jQuery javascript library to manipulate all you want, and the editor itself is pretty easy to change in almost every way.

yoda
Cheers, but I have done a lengthy comparison between TinyMCE, CKEditor and several others and have committed myself (and the client) to CKEditor, so in this case I will need specific advice on that particular product.
Pekka
Well, in that case, I can only think of using jQuery to listen the DOM and change whathever you need. Check listen() function in jQuery documentation, should give you the lights.
yoda
+6  A: 

If you are talking about CKEditor( version 3), then there is a possibility to get any DOM instance inside the editor itself. Every CKEditor instance has reference to it's document via "document" property.

var documentWrapper = edit.document;

This reference represent some public wrapper for all CKEditor nodes, but it also has the direct reference to its node. You can retrieve by getting ["$"] property.

var documentNode = documentWrapper.$; // or documentWrapper['$'] ;

documentNode will represent the DOM instance of the document node inside the iframe. After you have the DOM instance, you can do whatever you want to do with DOM structure, Append, remove, replace classes, rebuild, etc. For example

documentNode.body.className = "zork";

I hope this should be enough.

nemisj
edit.document? Where does edit get its value?
Upper Stage
edit - is the instance of CKEditor, if you create with replace function.
nemisj
Great, this works. Thank you!
Pekka
very nice answer
Upper Stage
@nemisj, I must have let the bounty pass by, maybe thinking your answer would be auto-accepted anyway. But apparently, to auto-accept, there must be two or more upvotes present, and now I can't even accept it the normal way. Sorry!!
Pekka
+2  A: 

From the Manual:

<static> {String|Array} CKEDITOR.config.contentsCss

The CSS file(s) to be used to apply style to the contents. It should reflect the CSS used in the final pages where the contents are to be used.

config.contentsCss = '/css/mysitestyles.css';
config.contentsCss = ['/css/mysitestyles.css', '/css/anotherfile.css'];

Default Value:

<CKEditor folder>/contents.css

Jonathan Sampson
Cheers Jonathan, but in my situation, I have a style sheet that applies only to bodies of a certain class name, this is why I need to set the body class specifically.
Pekka
A: 

Your queries may return undefined because the editor instances are placed inside an iFrame and your query is not looking there?

Upper Stage
+2  A: 

Although that part of the API wasn't ported from 2.x at the time that this question was placed, now it's easier to use the bodyId and bodyClass config options.

Of course, the explanation by nemisj is good and can be useful for other things, but you must remember that each time that you switch away from design (to source view), the iframe is destroyed, so you'll need to reassign your attributes if you do it manually.

AlfonsoML
Great stuff Alfonso, thanks!
Pekka
+1  A: 

I had the same problem today in trying to set the bodyClass like this:

CKEDITOR.replace( 'editor1', { fullPage : true, bodyClass : 'myClass'

});

What I found is that in this version (3.3.1), if you set fullpage = true, setting the bodyId or bodyClass does not work, but if you set fullPage = false, it does work.

Hope this helps.

Mark Landson