CKEditor for jQuery
The new jQuery Adapter is our first integration effort for popular JavaScript libraries. We're proposing features tightly integrated with jQuery, while not introducing too many new methods to it. This article illustrates it, including some sample code snippets.
Creating editor instances
To create editor instances, other than the usual CKEditor core script, you need to load the jQuery Adapter file in the page, in the following order:
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script> <script type="text/javascript" src="/ckeditor/adapters/jquery.js"></script>
At this point, any textarea, p or div element can be transformed into a rich text editor by simply using the ckeditor() method:
$( 'textarea.editor' ).ckeditor();jQuery chaining can also be used:
$( '.section-x' )
.find( 'textarea.editor' )
.ckeditor()
.end()
.find( 'a' )
.addClass( 'mylink' )
.end();The ckeditor() is the main method in the jQuery adapter. It accepts two optional parameters:
- A callback function to be execute when the editor is ready;
- Configuration options specific to the created editor instance.
The configurations options are passed through a simple object containing properties, each one related to a specific editor setting.
$('.jquery_ckeditor')
.ckeditor( function() { /* callback code */ }, { skin : 'office2003' } );The "this" reference inside the callback function points to the the CKEDITOR.editor object representing the editor instance.
Code interaction with editor instances
As soon as an editor instance is ready (after the above callback call), the ckeditorGet() method can then be used to retrieve a CKEDITOR.editor object that represents an editor instance. For example:
var editor = $('.jquery_ckeditor').ckeditorGet();
alert( editor.checkDirty() );
Because setting and retrieving the editor data is a common operation, the jQuery Adapter also provides the dedicated val() method:
// Get the editor data. var data = $( 'textarea.editor' ).val(); // Set the editor data. $( 'textarea.editor' ).val( 'my new content' );
This feature can be disabled by setting CKEDITOR.config.jqueryOverrideVal to false, before loading the adapter code.
For textareas, the editor will automatically return it's content back to the form when it is submitted. CKEditor also works with the official jQuery Form Plugin for AJAX based forms. It doesn't require anything from the developer side.
Events handling
Although CKEditor uses its own event system, there are four main events which we're exposing to the jQuery event system. All events use the event namespace, which is simply named ".ckeditor".
The following events are available:
- instanceReady.ckeditor: fired when the editor is created, but before any callback being passed to the ckeditor() method.
- setData.ckeditor: fired when data is set into the editor.
- getData.ckeditor: fired when data is fetched from the editor. The current editor data is also passed in the arguments.
- destroy.ckeditor: fired when the editor gets destroyed. It can be used, for example, to execute some cleanup on the page.
The editor instance is always passed as the first data argument for the listener. Both getData and setData are often used internally so listening to them should be done with care.
jQuery events DO bubble up through the DOM, so they can be listened selectively on certain parts of the document.
Feedback
Share your thoughts about our very first adapter. What's the best feature? What's missing? Which other adapters should we work on? Stay in touch!
Other Products
Browse for images and files in CKEditor, with thumbnails, easy to use and intuitive context menu.
Commercial License
For many companies and products, Open Source licenses are not an option. This is why the Closed Distribution License (CDL) has been introduced.

Comments
save icon
Im still playing around with the ckeditor. I cant figure out how when i click the save icon, it submits the form but when i look at the form data on the server side, the form field is empty.
Im looking at doing it via ajax and jquery, but i havent been successful in preventing the form to be submitted when you click on the save icon.
Any help is appreciated!
CKFinder
Works great by itself, but does not work with CKFinder integreation.
Cannot use multiple editors per page
I just made an attempt to upgrade from CKEditor 3.0 using the jquery plugin that was in development at the time to 3.1 and the bundled plug-in. I used to have layout issues and those are now fixed, which is very nice.
I initially came across an issue with not being able to instanciate multiple editors in the same page, or the same one multiple times for that matter. Turns out the problem was that my textarea elements did not have an id, which CKEditor seems to be using to manage the instances internally. Generating unique ids solved the issue.
The lack of error reporting made the issue hard to diagnose. There were no indications in the error console or FireBug.
So far, I am very satisfied with this release. My only suggestion would be to either make sure the id is generated on the fly if not present or a warning be reported.
IMPORTANT: CKEditor team..
The editor is really coool! I can understand the time and effort you guys have put to develop this, its really tremendous..
But one simple question i want to ask your team.. Why cant your team consider a least for the documentation. In the above documentation, it is said
"As soon as an editor instance is ready (after the above callback call), the ckeditorGet() method can then be used to retrieve a CKEDITOR.editor object that represents an editor instance. For example:
Your code goes here
"
But how will a user know the callback function is completed and the editor instance is ready to acccess? As simple as where and when i should call a property on editor instance like, editor.checkDirty() ????
I feel you guys need to give some importance to documentation too..a developer looking for a quick integration may get pissed off with this docos...you may loose some to TInyMCE..
Anyways, i like the editor so much and prefer using it....
You should use callback
You should use callback always when there's the POSSIBILITY of editor not being ready. Mostly it's up to you to know your code and what's happening when. For eg in multi-step form, in 5th step when editor is instanced in 2nd, it's almost sure that editor IS ready then. On the other hand, when everything is binded to onDocumentReady, you should use callback.
You can use callback even on existing editors and have them as many as you need to.
Checking if editor is ready is not so useful information, because you shouldn't write 2 versions of the same code. Although you can check if editor is ready like this:
$el.data( 'ckeditorInstance' ) && ! $el.data( '_ckeditorInstanceLock' )
You are also able to know when editor get's instanced using:
$el.bind( 'instanceReady.ckeditor', function( el ) { alert( this.name ) } )
JQuery 1.4
I've tried using the editor with JQuery 1.4, but it will not work. I can only get it to work using the online googleapis include. Also while typing this message if i try to past text its placed at the beginning of the file ?
Here is the code that works:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript" src="../ckeditor.js"></script>
<script type="text/javascript" src="../adapters/jquery.js"></script>
But this does not:
<script src="../../javascript/jquery-1.4.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="../ckeditor.js"></script>
<script type="text/javascript" src="../adapters/jquery.js"></script>
However normal JQuery slectors do work like this:
$('*[required]').addClass('test');
Thank you for report, i've
Thank you for report, i've created a new ticket for it: http://dev.fckeditor.net/ticket/5070 . Please add your email do CC field to be notified about updates on it.
It's important to note that googleapis contains jQuery 1.4.1.
Same happened here
I could not use the latest 1.4 release (hosted on Google).
It only worked with this include...
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
CKFinder
what about adding the CKfinder into this editor instance? How would you pass the CKFinder.SetupCKEditor function?
There shouldn't be any change
There shouldn't be any change for CKFinder integration. You can still do it in 2 ways:
1. Pass existing instance when it's available. Eg:
$( 'textarea.editor' ).ckeditor( function()
{
CKFinder.SetupCKEditor( this );
});
2. Just use SetupCKEditor without passing editor instance, then it will be automatically attached to every new one (using CKEditor event system).
add the code for reference
what about adding the CKfinder into this editor instance? How would you pass the CKFinder.SetupCKEditor function?
Paste whitout maintaining style
Hi.
Someone can tell me if is possible to paste text without the original style, or better, I need only to preserve the CSS font established in the general site declaration....
Thanks a lot.
Problem with jquery form plugin
I have a textarea with a complex name ie.
<textarea id="editor" name="edit[editor]">{$edit.text}</textarea>
and I use jQuery Form Plugin, the content of the ckeditor is not being passed to PHP:
$("#zapisz").click(function(){
$("form:first").ajaxSubmit();
});
unless I do it manually:
$("#zapisz").click(function(){
$("#editor").text($("#editor").val());
$("form:first").ajaxSubmit();
});
You're right, i've created a
You're right, i've created a ticket for it - http://dev.fckeditor.net/ticket/5046.
Good feature
The jQuery feature is really good, but
- I couldn't make the "CKEDITOR_BASEPATH" and "CKEDITOR_GETURL" function definition to work : they seem to be ignored.
- A missing feature is a function definition on the "save" event to do ajax post
could be : .ckeditor( function() { /* callback */ }, { <param>}, function(data){/* code to manage data with ajax*/} )
Such things
Such things as CKEDITOR_BASEPATH are not touched by adapter at all, but generally you have to set it before loading CKEditor.
What "save" function you have on mind ?
Looks good, don't need two public methods
There's no real need for a public ckEditorGet method. You can just tell users to access the full underlying instance by storing it in .data("ckeditor")
var editor = $("textarea").ckeditor().data("ckeditor");
Right, there's no need for
Right, there's no need for ckeditorGet() although it's more intuitive and easier to document, so we've decided to provide it as the second one.
Now you can get to editor instance binded to element by using .data( 'ckeditorInstance' ). Although running it right after ckeditor() method most probably will end in error.
very excite
very good,I blive it a good one ,evry one will like it.
thank u.
Post new comment