How can you change this
<div id='myDiv'><p>This div has <span>other elements</span> in it.</p></div>
into this
<div id='myDiv'>This div has other elements in it.</div>
hopefully using something like this
var ele = document.getElementById('myDiv');
while(ele.firstChild) {
replaceFunction(ele.firstChild, ele.firstChild.innerHTML);
}
function replaceFunction(element, text) {
// CODE TO REPLACE ELEMENT WITH TEXT
}
You can use
innerTextandtextContentif you want to remove all descendant nodes, but leave the text:If you only want to flatten certain ones, you can define:
should pull all the children out of
eleinto its parent, and removeele.So if
eleis thespanabove, it will do what you want. To find and fold all the element children of#myDivdo this: