You can create a function that will create a new div element, place it before the first child element you want to wrap, then you can append the child elements you want to wrap to the newly created parent div. Here is my solution for my specific case so you can modify it. Here, I wanted to wrap all of the sections with class “section-recent-titles” within a parent div.
// Wrap sections in a wrapper.
function wrapSections(section_class) {
var childSections = document.querySelectorAll("."+section_class);
var wrapper = document.createElement('div');
wrapper.classList.add(section_class+"-wrapper");
childSections.item(0).parentNode.insertBefore(wrapper, childSections.item(0));
for ( var i=0; i< childSections.length; i++){
wrapper.appendChild(childSections.item(i));
}
}
// You can call this function with a specific section class.
wrapSections("section-recent-titles");
You would put this code at the end of your theme’s JavaScript file, or better yet in a custom JavaScript file for your store.
You should be able to copy the wrapSections function exactly as written. And then call the function with the last line, substituting “section-recent-titles” with “your-parent-section-name”. Every theme is different, so it might need some adjustments.