How to limit the number of words in a textarea in summernote

Viewed 4773

I'm trying to limit the number of words allowed in a summernote.

example, I want to limit up to 50 words only.

Do you have any idea?

$(document).ready(function() {
    $('#summernote').summernote({
        height: 20,
   });
});
3 Answers
var maxwords = 100;
 $(".summernote").summernote({
        height: '200px',
          callbacks: {
              
                onKeydown: function (e) {
                 var t = e.currentTarget.innerText;   
                 var words = t.split(" ");
                 if (words.length >= maxwords) {
                      //delete key
                  if (e.keyCode != 8)
                    e.preventDefault();
                    // add other keys ...
                  }
                },
                 onKeyup: function(e) {
                    var t = e.currentTarget.innerText;
                     var words = t.split(" ");
                   if (words.length >= maxwords) {
                      //delete key
                  if (e.keyCode != 8)
                    e.preventDefault();
                    // add other keys ...
                  }
                },
                onPaste: function(e) {
                var t = e.currentTarget.innerText;
                var bufferText = ((e.originalEvent || e).clipboardData || 
                                   window.clipboardData).getData('Text');
                e.preventDefault();
                var all = t + bufferText;
                var words = all.split(" ");
                var array = words.slice(0, maxwords)
                document.execCommand('insertText', false, array.join(" "));
               
             }
        }
    });
Related