How can I not enter more than 10 characters with WYSIWYG?

1

Follow the code:

Html:

<textarea id="summernote">Escrever aqui</textarea>
<h5 id="limite_vermelho" style="text-align:right;color:red"></h5>
<h5 id="limite_normal" style="text-align:right"></h5>

JS:

$('#summernote').on('summernote.keyup', function(e) {
    debugger;
    var text = $(this).next('.note-editor').find('.note-editable').text();
    var length = text.length;
    var num = 10 - length;

    if (length > 10) {
        $('#limite_normal').hide();
        $('#limite_vermelho').text(10 - length).show();
    }
    else{
        $('#limite_vermelho').hide();
        $('#limite_normal').text(10 - length).show();
    }

});

Or if you prefer in jsfiddle: link

It can not enter more than 10 characters, how can I do this with jquery?

    
asked by anonymous 05.04.2017 / 00:53

1 answer

1

Summernote has several callback functions in its documentation. Among them, onKeyup . So, just include this callback when starting the object:

$('#summernote').summernote({
  callbacks: {
  onKeydown: function(e) {
   var textarea = $(this).next('.note-editor').find('.note-editable');
   var texto = $(textarea).text();

   if (texto.length > 10) {
     // apaga o texto que foi digitado
     $('#summernote').summernote('reset');
     // insere o texto dentro do limite desejado (no caso 10)
     $('#summernote').summernote('insertText', texto.substr(0, 10));
   }
  }
 }
});
    
05.04.2017 / 02:01