javascriptjquerydom

Remove a tag without deleting content


I am wondering if it is possible to remove a tag but leave the content intact? For example, is it possible to remove the SPAN tag but leave SPAN's content there?

<p>The weather is sure <span>sunny</span> today</p> //original

<p>The weather is sure sunny today</p> //turn it into this

I have tried using this method of using replaceWith(), but it turned the HTML into

<p>
  "The weather is sure " 
  "sunny"
  " today"
</p>

Solution

  • jQuery has easier ways:

    var spans = $('span');
    spans.contents().unwrap();
    

    With different selector methods, it is possible to remove deeply nested spans or just direct children spans of an element.