Search code examples
jqueryonkeyupsummernote

Getting last entered text from summernote


I want to know the last word written or pasted on summernote, below is the code I am using for the same

onkeyup: function (e) {


              //  CurrentContent = $('#summernote').code();
                var CurrentContent = $('#summernote').code().replace(/(<([^>]+)>);/ig, "");
                if (e.keyCode == 32) {
                    PrevContent = CurrentContent.replace(PrevContent, "");

                    $('#content').text(PrevContent);
                    PrevContent = CurrentContent;
                }

                console.log('Key is released:', e.keyCode);
            }



<div id="summernote"></div>
 <h1>Latest word:</h1>
<div id="content"> </div>

This is not giving me correct result; instead it is giving me something like this when I am typing "Test "

<p>Test&nbsp;</p>

How can i fix it? sorry for my bad english


Solution

  • To get the latest word simply use:

    var latestword = $('#summernote').text().split(/(\s|&nbsp;)/).pop();
    

    Since you did not provide any html I dont know if .text() is the right function, replace it with .html() or whatever .code() does