Last active
June 16, 2019 07:22
-
-
Save Daniel-Hug/de9a165a6d9c74686854 to your computer and use it in GitHub Desktop.
JS: unwrap element (remove parent without removing child nodes). Demo: http://jsbin.com/hipahu/edit?html,js,output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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