John Smith John Smith - 1 year ago 359
Javascript Question

How to get the plain text from summernote editor?

for example, this is what I entered:


converts it to

or another thing:



<span class="message_content">[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]</span>

how to get the pure / plain text?

Answer Source

You could apply one of the top two answers for the question JavaScript: How to strip HTML tags from string?, after the .code() to strip the tags.

ReactiveRaven's answer (to keep it to one line) works fine for me:

cleanText = $("#summernote").code().replace(/<\/?[^>]+(>|$)/g, "");

For example, With [{"_type":"ServerOperation","operationType":"ANNOUNCE"}]:

  • $("#summernote").code() returns


  • $("#summernote").code().replace(/<\/?[^>]+(>|$)/g, "") returns


    without any tags.

And if you want to preserve the carriage return, you could replace </p> and <br> for a \n before applying the solution specified on the linked question:

                .replace(/<\/p>/gi, "\n")
                .replace(/<br\/?>/gi, "\n")
                .replace(/<\/?[^>]+(>|$)/g, "");