this post was submitted on 12 Jun 2023
9 points (100.0% liked)

WebDev

1142 readers
1 users here now

Community for all things Web Development related.

founded 1 year ago
MODERATORS
 
top 3 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 1 year ago

Just to make your pain a little less...

function collapseNestedDivs(element) {
  const childDivs = element.querySelectorAll('div');

  childDivs.forEach((div) => {
    collapseNestedDivs(div); // Recursively check for nested divs

    if (div.childElementCount === 1 && div.children[0].tagName === 'DIV') {
      const nestedDiv = div.children[0];
      const parent = div.parentNode;

      // Move the styles from the nested div to the parent div
      Array.from(nestedDiv.style).forEach((styleName) => {
        parent.style[styleName] = nestedDiv.style[styleName];
      });

      // Move the classes from the nested div to the parent div
      nestedDiv.classList.forEach((className) => {
        parent.classList.add(className);
      });

      // Remove the nested div
      parent.removeChild(nestedDiv);
    }
  });
}

// Call the function to collapse the nested divs starting from a root element
const rootElement = document.getElementById('root'); // Replace 'root' with your actual root element ID
collapseNestedDivs(rootElement);

[–] [email protected] 2 points 1 year ago (1 children)
[–] [email protected] 2 points 1 year ago* (last edited 1 year ago)

My condolences. I'm having war flashbacks of working on an early 2000 website where the owner had repeatedly mashed "center" to try and get centered text in table cells. In every cell. 100+ center tags.