How to replace element with text using JavaScript / jQuery?

dom, html, javascript, jquery

Solution

`.replaceWith` ...

$( "#foo" ).replaceWith( "bar" );

jQuery: Documentation »

Problem

So assuming I had this: ``` <span id="foo">bar</span> ``` I want to replace it with this: ``` bar ``` In other words, I want to remove the span from the DOM, and insert its contents in its place. How can I achieve this, using code as minimal as possible? I want to use pure JavaScript, but jQuery's fine as well. Thanks.

Original source

Related problems