I want to remove all children from a div node and for each paragraph child removed, I want add them to a new paragraph element so that all paragraphs are merged into one and anything else is removed.
However, while removing all paragraphs and adding them to this new paragraph, I also want to remove all images (and not just by removing the opening and closing arrows) and make sure the format is kept the same for all words (just 11pt standard font).
I hope that makes sense. Here is what I have so far:
var newP = document.createElement('P');
var paras = comment.getElementsByTagName('P');
var counter = 0;
while (paras[0]) {
var next = paras[0]; // needs to be 0;
if (next.innerHTML.length > 8) { // to safely ignore
counter++;
if (counter < 3)
newP.innerHTML += next.innerHTML + "<br /><br />";
}
comment.removeChild(next); // comment is the div which holds the new para
next = paras[0];
if (!next)
newP.innerHTML += "<a href='" + link + "'>click to view full entry</a>";
}
comment.appendChild(newP);
Here is a sample of the HTML Mark up which needs to be formatted:
<div class="comment">
<h3>Heading</h3>
<p style="font-weight: bold">This has been formatted which should be removed. <span style="font-size: 14pt; color: red;"> This also includes all span tags!</span></p>
<p>This is a para <a href="">with an anchro</a></p>
<div> this is a div with an image <img src="//placehold.it/64X64" /></div>
<p>This is an image in the middle of a paragraph <img src="http://www.thinkstockphotos.com.au/CMS/StaticContent/Hero/TS_AnonHP_462882495_01.jpg"/> which I want to remove, and not just the arrows</p>
</div>
And I want it to look like this:
<div class="comment">
<p>Heading</p>
<p>This has been formatted which should be removed. This also includes all span tags!</p>
<p>This is a para with an anchro</p>
<div> this is a div with an image</div>
<p>This is an image in the middle of a paragraph which I want to remove, and not just the arrows</p>
</div>
EDIT: This is not a duplicate as I want to completely remove all images and all attributes/text between the image opening and closing tags (not just remove the arrows that make up the tag) and remove all formatting!