I’m trying to do something similar/same as in this question:
How to remove only the parent element and not its child elements in JavaScript?
<div>
This is some text here
<h2>This is more text</h2>
</div>
All I want is to remove the H2 tag. The result should be:
<div>
This is some text here
This is more text
</div>
Assume that I have the H2 element already:
if (parentElement.nodeName.toLowerCase() == "h2") {
//now what? I basically want to this: $.replaceWith(parentElement.innerText)
//just without jQuery
}
Assuming the variable
h2accurately references theh2element you want to act upon, my first thoughts would be:JS Fiddle demo.
To make it slightly more DRY, a function approach could be:
JS Fiddle demo.
Adjusted the above in response to Felix Kling’s comments (below), and also to use
replaceChild():JS Fiddle demo.