CKEditor 4.5.1 – get selection on mouse click and keyboard

by Juan Muñoz

HTML

<script src="http://cdn.ckeditor.com/4.5.1/full/ckeditor.js"></script>
<textarea id="editor">
    <p>Hello</p>
    <p>World</p>    
</textarea>

JavaScript

function logSelection() {
    console.log( this.getSelection() );
}

CKEDITOR.replace( 'editor', {
    on: {
        contentDom: function() {
            this.document.on( 'mouseup', logSelection, this );
            this.document.on( 'keyup', logSelection, this );
        }
    }
} );