Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug Daniel-Hug/unwrap.js
Last active Jun 16, 2019

Embed
What would you like to do?
JS: unwrap element (remove parent without removing child nodes). Demo: http://jsbin.com/hipahu/edit?html,js,output
// remove parent without removing childen
function unwrap(wrapper) {
// place childNodes in document fragment
var docFrag = document.createDocumentFragment();
while (wrapper.firstChild) {
var child = wrapper.removeChild(wrapper.firstChild);
docFrag.appendChild(child);
}
// replace wrapper with document fragment
wrapper.parentNode.replaceChild(docFrag, wrapper);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.