Alexander Weihmayer
Alexander Weihmayer

Reputation: 376

CKEditor - How to check if textarea is empty on event?

I'm trying to check if the textarea is empty using an event

However, you can't add events like you usually would with jQuery or Javascript to a CKEditor and getting the value is not as easy as document.getElementById("TEXT_AREA_ID").value. I was wondering if there is a workaround for CKEditors to do this?

Upvotes: 1

Views: 3996

Answers (1)

Alexander Weihmayer
Alexander Weihmayer

Reputation: 376

It took quite a bit of searching but I found some code that adds an event to a CKEditor and that is able to get the value of the text area.

CKEDITOR.on('instanceCreated', function(e) {
    e.editor.on('contentDom', function() {
        e.editor.document.on('keyup', function(event) {
            if(CKEDITOR.instances.TEXT_AREA_ID.getData() == ""){
                //Do something if textarea is empty
            }
        });
    });
}); 

You need to do a few extra steps by calling the CKEditor before you add a regular event. You can get the value of the text area by calling the CKEDITOR.instances.TEXT_AREA_ID.getData() to return a string.

Upvotes: 2

Related Questions