Skip to content

Instantly share code, notes, and snippets.

@erkobridee
Created February 26, 2026 10:28
Show Gist options
  • Select an option

  • Save erkobridee/5cde215dea145a7839bb633340084255 to your computer and use it in GitHub Desktop.

Select an option

Save erkobridee/5cde215dea145a7839bb633340084255 to your computer and use it in GitHub Desktop.
// ref.: https://plainjs.com/javascript/manipulation/unwrap-a-dom-element-35/
/**
* unwrap the content of a given HTMLElement to the parent element at the previous
* position of the given element
*
* @param {HTMLElement} element
* @param {boolean} removeElement
*/
export const unwrap = (element: HTMLElement, removeElement = true) => {
const parentNode = element.parentNode;
if (!parentNode) return;
while (element.firstChild) parentNode.insertBefore(element.firstChild, element);
if (removeElement) parentNode.removeChild(element);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment