Abs Abs - 1 month ago 11
Javascript Question

Remove parent but keep children using jQuery

I would like to remove the parent and keep the children in my HTML using jQuery. This works:

$('#my_span').children().insertBefore('#my_span').end().end().remove();


However, it removes the text and comment node types - how can I amend this so that I keep the text?

Happy to do this with pure Javascript too.

Answer

As @Cᴏʀʏ says, unwrap() should help you acheive this.

Alternatively, you could do something like this:

$('#my_span').parent().html($('#my_span').html());
Comments