Skip to content

Instantly share code, notes, and snippets.

@jgoslow
Created August 13, 2022 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgoslow/c6a521ca9c880799654ebe7b0cb5c235 to your computer and use it in GitHub Desktop.
Save jgoslow/c6a521ca9c880799654ebe7b0cb5c235 to your computer and use it in GitHub Desktop.
Unwrap an element from it's container
/**
* Unwrap element
*/
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