Wrap an element around a given element Wrap the wrapper element around the ele element: // First, insert `wrapper` before `ele` in its parent node ele.parentNode.insertBefore(wrapper, ele); // And then, turn `ele` into a children of `wrapper` wrapper.appendChild(ele); Source https://htmldom.dev/wrap-an-element-around-a-given-element