ckeditor

Getting started with ckeditor

Remarks#

CKEditor is a JavaScript based WYSIWYG editor created for use within web pages. It is open source and plugin based making it both customizable and extensible.

The CKEditor website can be found in https://www.ckeditor.com and the source code of the library is available on github.

The official documentation of CKEditor can be found at https://docs.cksource.com/Main_Page

Versions#

VersionNotesRelease Date
4.2JQuery Adapter, HiDPI icons, dropped support for Internet Explorer 7 and Firefox 3.62013-07-25
4.2.12013-09-10
4.2.22013-10-14
4.2.32013-11-14
4.3Widget System, Internet Explorer 11 Support2014-11-14
4.3.12013-12-10
4.3.22014-01-21
4.3.32014-02-25
4.3.42014-03-25
4.3.52014-04-24
4.4Code Snippet plugin, Enhanced Image plugin, Advanced Content Filter2014-04-24
4.4.12014-05-20
4.4.22014-06-24
4.4.32014-07-15
4.4.42014-08-20
4.4.52014-09-25
4.4.62014-11-25
4.4.72015-01-27
4.4.82015-07-02
4.5files drag&drop support, integration with the native clipboard, new low level APIs2015-07-02
4.5.22015-08-04
4.5.32015-08-19
4.5.42015-10-06
4.5.52015-11-12
4.5.62015-12-09
4.5.72016-02-04
4.5.82016-03-31
4.5.92016-05-13
4.5.102016-07-13
4.5.112016-09-08
4.6new default skin, Paste from Word rewritten, new plugins: Upload File, Balloon Panel2016-11-15
4.6.12016-12-08

Getting Started

Create a file ckeditor.html with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>CKEditor Demo!</title>
</head>
<body>
    <script src="//cdn.ckeditor.com/4.6.1/basic/ckeditor.js"></script>
    <div id="editor1">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.<br /><br />
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.<br /><br />
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.<br /><br />
    </div>
    
    <script>
    CKEDITOR.replace( 'editor1' );
    </script>
</body>
</html>

Live Demo on JSFiddle

Explanation of code

  1. Loads the CKEditor library from the CKEditor CDN:

        <script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.6.1/ckeditor.js"></script>
  2. Create a new DIV element with all of the content that we want inside the Editor

    <div id="editor1">
    ALL CONTENT HERE
     </div>
  3. Tell the CKEDITOR object to replace the element with the id editor1 with a WYSIWYG editor (the CKEditor).

     <script>
     CKEDITOR.replace( 'editor1' );
     </script>

For more refer to the CKEditor Documentation page.

CKEditor - Inline Editor Example

Create a file ckeditor-inline.html with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>CKEditor Inline Demo!</title>
</head>
<body>
    <script src="//cdn.ckeditor.com/4.6.1/basic/ckeditor.js"></script>
    <div id="editor1" contenteditable="true">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.<br /><br />
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.<br /><br />
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.<br /><br />
    </div>
</body>
</html>

Live Demo on JSFiddle

Explanation of code

  1. Loads the CKEditor library from the CKEditor CDN:

        <script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.6.1/ckeditor.js"></script>
  2. Create a new DIV element with all of the content that we want inside the Editor

    <div id="editor1" contenteditable="true">
    ALL CONTENT HERE
     </div>

The important thing to note here is the contenteditable="true" inside the DIV element. The CKEditor will automatically replace every element that has contenteditable attribute with an inline editor.

For more refer to the Documentation.

Get the HTML content of CKEditor

Create a file ckeditor-content.html with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>CKEditor Get Content Demo!</title>
</head>
<body>
    <script src="//cdn.ckeditor.com/4.6.1/basic/ckeditor.js"></script>
    <div id="editor1" contenteditable="true">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.<br /><br />
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.<br /><br />
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.<br /><br />
    </div>
    <button id="btn1">Click to get the content</button>
    <script>
        document.getElementById('btn1').addEventListener('click', function() {
            content = CKEDITOR.instances.editor1.getData()
            console.log(content);
        }, false);
    </script>
</body>
</html>

Live Demo on JSFiddle

Explanation of code

  1. Loads the CKEditor library from the CKEditor CDN:

        <script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.6.1/ckeditor.js"></script>
  2. Create a new DIV element with all of the content that we want inside the Editor

    <div id="editor1" contenteditable="true">
    ALL CONTENT HERE
     </div>
  3. Add a click listener to the button we have, and once clicked - get the HTML content of the ckeditor.

     <script>
         document.getElementById('btn1').addEventListener('click', function() {
             content = CKEDITOR.instances.editor1.getData()
             console.log(content);
         }, false);
     </script>
  1. The name of the editor editor1 in CKEDITOR.instances.editor1 is the value of the id attribute of the element we used (<div id="editor1" ... >)
  2. Note the usage of console.log - the HTML content of the editor will be available in the console (you can open the console by clicking the F12 key)

For more refer to the Inline Documentation page.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow