Why is my p tags getting stripped out after parseHTML?

117 views Asked by At

I am using summernote, pulling out the content in the textarea and trying to save to database. However before saving to the database, I am stripping out the img base64 craziness and replacing the image tags source.

However, when I print out the original source received from the textarea by doing. var mytext = $('#myid').summernote('code'); that all displays fine.. showing my paragraphs and an image (in base64).

But, if I use jQuery, run that through parseHTML, and attempt to read back the data, I get the following results:

var mymodedtxt = $(mytext).html(); - I only get the first paragraph of text (printed to console)

$(mytext).children().prop('outerHTML'); - I only get the first image with the new path I modified.

<img src="/path/to/22019_5859a7b13a78c.jpeg" />

Before I print these out, I am modding my image paths. I am only doing this:

var i = 0;
$(mytext).find('img').each(function() { 
    $(this).attr('src',newpath[i]);
    i++;
}); 

Does anyone have any clue what could be causing this? Why are my p tags getting stripped out?

1

There are 1 answers

2
Barmar On BEST ANSWER

When you call .html() on a jQuery collection, it only returns the HTML of the first element in the collection.

What you should do is wrap the HTML string in a container element, e.g. a DIV, then get its HTML:

var mymodedtxt = $("<div>", { html: mytext }).html();