maxlength plugin on ckeditor does not work

1

I need to limit the amount of characters in CKEditor.

I followed the step by step of this topic: Limit text in CKEDITOR

But it did not work. Below the changes made to my code:

<textareaid="desc" name="desc" data-maxlen="10" rows="7" cols="45"</textarea>

The ckeditor works, but the plugin does not.

Ps. I also tried to add the "maxlenght" folder inside this plugin folder. It also did not work.

Something wrong with your implementation?

Another question: How does this plugin work? it does not let pass the characters of the chosen quantity? or does it display error after submitting the form?

Thank you in advance.

    
asked by anonymous 03.04.2018 / 14:00

1 answer

1

You can use this code below. It will get the value in maxlength and limit the amount of characters in CKEditor.

Textarea:

<textarea name="editor1" maxlength="10" id="desc" rows="10" cols="80"></textarea>

Code:

<script>
window.onload = function(){            
    CKEDITOR.instances.desc.on('key',function(event){
       // cada código na array representa uma tecla
       // Ex.: backspace, delete, uparrow etc..
       var keys = [46,8,38,40,37,39,35,36,16,18];
        var keyCode = event.data.keyCode;
        if(~keys.indexOf(keyCode)){
            return true;
        }else{
            var textLimit = document.querySelector("#desc").getAttribute("maxlength");
            var str = CKEDITOR.instances.desc.getData().replace(/<[^>]*>/g, "");
            if (str.length >= textLimit) return false;
        }
    });    
};

CKEDITOR.replace('desc');
</script>
    
03.04.2018 / 16:10