How to Remove a Parent Element without Removing its Child using JavaScript?

Estimated read time 1 min read

To remove a parent element without removing its child elements in JavaScript, you can use the remove() method on the parent element. Here’s an example:

let parent = document.querySelector("#parent");
let child = parent.querySelector("#child");

let newParent = child.parentNode;

This code removes the parent element with the ID of "parent" from the DOM without removing its child element with the ID of "child". The child element is then reparented to the node from which the original parent was removed.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply