Why is my p tags getting stripped out after parseHTML?

261
January 13, 2017, at 03:21 AM

So I am using summernote, pulling out the content in the textarea and trying to save to database. However before saving to db, 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 using 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.

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?

Answer 1

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();
READ ALSO
Multi-item carousel bootstrap4

Multi-item carousel bootstrap4

I want to make a Carousel that contains multi items or images, it will display 4 items then when I press on next a new item will appear and so onThis image describes what I want :

379
How do I override Bootstrap&#39;s class to increase the navbar height?

How do I override Bootstrap's class to increase the navbar height?

Currently my navbar is about 50px highI want it to be around 150px

296
javascripts inside &lt;body&gt; does not run after running .load function

javascripts inside <body> does not run after running .load function

I encounter an strange problem in javascript and could not find solution

328
jquery Ajax request HTML file e.lengthComputable false

jquery Ajax request HTML file e.lengthComputable false

I am trying to make a jquery ajax request (with progress bar) to get the content of a HTML fileThe way I need to do it, is when scroll reach bottom of main container then loads the new content

451