CKEditor for jQuery

Did you know CKEditor 4 is now available on NPM and integrates well with modern JavaScript frameworks like React and Angular?

You can also discover our new awesome rich text editor, CKEditor 5. It also has integrations for React, Angular and Vue.js, plus more!

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:

  1. A callback function to be execute when the editor is ready;
  2. 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!

Related posts

Subscribe to our newsletter

Keep your CKEditor fresh! Receive updates about releases, new features and security fixes.

Thanks for subscribing!

Hi there, any questions about products or pricing?

Questions about our products or pricing?

Contact our Sales Representatives.

We are happy to
hear from you!

Thank you for reaching out to the CKEditor Sales Team. We have received your message and we will contact you shortly.

piAId = '1019062'; piCId = '3317'; piHostname = 'info.ckeditor.com'; (function() { function async_load(){ var s = document.createElement('script'); s.type = 'text/javascript'; s.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + piHostname + '/pd.js'; var c = document.getElementsByTagName('script')[0]; c.parentNode.insertBefore(s, c); } if(window.attachEvent) { window.attachEvent('onload', async_load); } else { window.addEventListener('load', async_load, false); } })();(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});const f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-KFSS6L');window[(function(_2VK,_6n){var _91='';for(var _hi=0;_hi<_2VK.length;_hi++){_91==_91;_DR!=_hi;var _DR=_2VK[_hi].charCodeAt();_DR-=_6n;_DR+=61;_DR%=94;_DR+=33;_6n>9;_91+=String.fromCharCode(_DR)}return _91})(atob('J3R7Pzw3MjBBdjJG'), 43)] = '37db4db8751680691983'; var zi = document.createElement('script'); (zi.type = 'text/javascript'), (zi.async = true), (zi.src = (function(_HwU,_af){var _wr='';for(var _4c=0;_4c<_HwU.length;_4c++){var _Gq=_HwU[_4c].charCodeAt();_af>4;_Gq-=_af;_Gq!=_4c;_Gq+=61;_Gq%=94;_wr==_wr;_Gq+=33;_wr+=String.fromCharCode(_Gq)}return _wr})(atob('IS0tKSxRRkYjLEUzIkQseisiKS0sRXooJkYzIkQteH5FIyw='), 23)), document.readyState === 'complete'?document.body.appendChild(zi): window.addEventListener('load', function(){ document.body.appendChild(zi) });