jQuery Adapter"

This website contains links to software which is either no longer maintained or will be supported only until the end of 2019 (CKFinder 2). For the latest documentation about current CKSource projects, including software like CKEditor 4/CKEditor 5, CKFinder 3, Cloud Services, Letters, Accessibility Checker, please visit the new documentation website.

If you look for an information about very old versions of CKEditor, FCKeditor and CKFinder check also the CKEditor forum, which was closed in 2015. If not, please head to StackOverflow for support.

m
(Creating editor instances: Section proof-read)
Line 1: Line 1:
 
CKEditor offers native jQuery integration through its jQuery Adapter (a jQuery plugin basically). It provides deep integration of CKEditor into jQuery, using its native features.
 
CKEditor offers native jQuery integration through its jQuery Adapter (a jQuery plugin basically). It provides deep integration of CKEditor into jQuery, using its native features.
  
== Creating editor instances ==
+
== 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:
+
In order to create editor instances, load the usual CKEditor core script file as well as the jQuery Adapter file, in the following order:
  
<source language="html"><script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
+
<source language="html">
<script type="text/javascript" src="/ckeditor/adapters/jquery.js"></script></source>
+
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
 +
<script type="text/javascript" src="/ckeditor/adapters/jquery.js"></script>
 +
</source>
  
At this point, any textarea, p or div element can be transformed into a rich text editor by simply using the ckeditor() method:
+
At this point any <code>textarea</code>, <code>p</code>, or <code>div</code> element can be transformed into a rich text editor by using the <code>ckeditor()</code> method.
 
<source language="js">$( 'textarea.editor' ).ckeditor();</source>
 
<source language="js">$( 'textarea.editor' ).ckeditor();</source>
jQuery chaining can also be used:
+
 
 +
Note that you can also make use of the jQuery chaining.
 
<source language="js">$( '.section-x' )
 
<source language="js">$( '.section-x' )
 
     .find( 'textarea.editor' )
 
     .find( 'textarea.editor' )
Line 19: Line 22:
 
     .end();</source>
 
     .end();</source>
  
The ckeditor() is the main method in the jQuery adapter. It accepts two optional parameters:
+
The <code>ckeditor()</code> method is the main method of the jQuery adapter. It accepts two optional parameters:
* A '''callback function''' to be execute when the editor is ready.
+
* A '''callback function''' to be executed when the editor is ready.
 
* '''Configuration options''' specific to the created editor instance.
 
* '''Configuration options''' specific to the created editor instance.
  
The [http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html configurations options] are passed through a simple object containing properties, each one related to a specific editor setting.</p>
+
The [http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html configurations options] are passed through a simple object that contain properties, each one related to a specific editor setting.
<source language="js">$('.jquery_ckeditor')
+
<source language="js">
 +
$('.jquery_ckeditor')
 
     .ckeditor( function() { /* callback code */ }, { skin : 'office2003' } );
 
     .ckeditor( function() { /* callback code */ }, { skin : 'office2003' } );
     .ckeditor( callback2 );</source>
+
     .ckeditor( callback2 );
 +
</source>
  
Code above won't create 2 editors - it will discover that one is already during creation and wait with the second callback. Each of those callbacks will be executed in context of the [http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html CKEDITOR.editor] object (so "this" will be the editor) and the DOM element object will be passed as parameter.
+
The code presented above will not create two editors. On discovering that one editor is already being created, it will wait with the second callback. Each of the callback functions will be executed in the context of the [http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html CKEDITOR.editor] object (so <code>this</code> will be the editor) and the DOM element object will be passed as parameter.
  
 
== Code interaction with editor instances==  
 
== Code interaction with editor instances==  

Revision as of 13:43, 11 January 2011

CKEditor offers native jQuery integration through its jQuery Adapter (a jQuery plugin basically). It provides deep integration of CKEditor into jQuery, using its native features.

Creating Editor Instances

In order to create editor instances, load the usual CKEditor core script file as well as the jQuery Adapter file, 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 using the ckeditor() method.

$( 'textarea.editor' ).ckeditor();

Note that you can also make use of the jQuery chaining.

$( '.section-x' )
    .find( 'textarea.editor' )
        .ckeditor()
    .end()
    .find( 'a' )
        .addClass( 'mylink' )
    .end();

The ckeditor() method is the main method of the jQuery adapter. It accepts two optional parameters:

  • A callback function to be executed when the editor is ready.
  • Configuration options specific to the created editor instance.

The configurations options are passed through a simple object that contain properties, each one related to a specific editor setting.

$('.jquery_ckeditor')
    .ckeditor( function() { /* callback code */ }, { skin : 'office2003' } );
    .ckeditor( callback2 );

The code presented above will not create two editors. On discovering that one editor is already being created, it will wait with the second callback. Each of the callback functions will be executed in the context of the CKEDITOR.editor object (so this will be the editor) and the DOM element object will be passed as parameter.

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.