Search code examples
javascripthtmlckeditorkeyeventonkeydown

CKEditor key event not updating text properly


I have the following code to automatically update the content inside a div when the user types it inside a CKEditor textarea:

CKEDITOR.instances.editor.on("key", function(e)
{
    var preview = document.getElementById('some-div');
    preview.innerHTML = CKEDITOR.instances.editor.getData();
});

The problem is that if I type in "Hello world", in the div it appears "Hello worl" and the "d" doesn't appear until another key is pressed. And I would want the same content in both places. Thanks in advance!


Solution

  • I solved it handling the key event in a different way. You can see it below:

    CKEDITOR.instances.editor.on('contentDom', function() 
    {
        CKEDITOR.instances.editor.document.on('keyup', function(event) 
        {
            var preview = document.getElementById('some-div');
            preview.innerHTML = CKEDITOR.instances.editor.getData();
        });
    });