Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the plain text from summernote editor?

for example, this is what I entered:

sdf
42342
xxcv

.code() converts it to sdf<br>42342<br>xxcv

or another thing:

[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]

becames

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

how to get the pure / plain text?

like image 948
John Smith Avatar asked Apr 09 '15 12:04

John Smith


3 Answers

Just try this:

var plainText = $($("#summernote").code()).text()

EDIT: In newer versions you need use this instead:

var plainText = $($("#summernote").summernote("code")).text()
like image 172
Dilshod Zopirov Avatar answered Sep 22 '22 14:09

Dilshod Zopirov


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

    <p>[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]<br></p>

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

    [{"_type":"ServerOperation","operationType":"ANNOUNCE"}]

    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:

$("#summernote").code()
                .replace(/<\/p>/gi, "\n")
                .replace(/<br\/?>/gi, "\n")
                .replace(/<\/?[^>]+(>|$)/g, "");
like image 23
Alvaro Montoro Avatar answered Sep 21 '22 14:09

Alvaro Montoro


I needed to check if the textarea had any content. This have done the trick:

Current summernote version (8):

$($("#summernote").summernote('code').replace(/&nbsp;|<br>/g, ' ')).text().trim() == ''

In my older version:

$($("#summernote").code().replace(/&nbsp;|<br>/g, ' ')).text().trim() == ''
like image 21
Gli Avatar answered Sep 24 '22 14:09

Gli