Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="accordion accordion-flush" id="bodycontainer-0">
</div>
              
            
!

CSS

              
                body {
  margin: 100px auto;
  box-shadow: 1px 34px 50px -17px #6a72b5a1;
  border-radius: 20vh;
  max-width: 800px;
  transition: all ease 0.4s;
}
body:hover {
  margin-top: 80px;
  box-shadow: 1px 64px 60px -17px #6a72b5a1;
}

<!-- Accordion Styles -- > <style > .accordion-header,
.accordion-item,
.accordion-button,
#bodycontainer-0,
#userHtmlFrame {
  background: #f0f0f0;
}

.accordion-button {
  box-shadow: inset 0 -1px 0 rgb(0 0 0 / 13%);
}

/* header styles */
.accordion-header button {
  text-decoration: none !important;
  color: #000000;
  font-family: "Montserrat";
  font-variant: normal;
  font-weight: 500;
  vertical-align: baseline;
  font-size: 30pt;
  text-transform: uppercase !important;
}

/* sub header styles */
.accordion-body .accordion-header .accordion-button {
  font-size: 16px;
}

/* plus sign */
.accordion-button::after {
  background-image: url("data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath d='M24 10h-10v-10h-4v10h-10v4h10v10h4v-10h10z'/%3E%3C/svg%3E");
}

/* dropdown arrow  */
.hasBody:after {
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23212529'%3e%3cpath fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3e%3c/svg%3e") !important;
}

/* minus sign */
.accordion-button:not(.collapsed)::after {
  background-image: url("data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24'%3E%3Cpath d='M0 10h24v4h-24z'/%3E%3C/svg%3E");
}

.onlyBody {
  padding: 15px;
  background: #ffffff;
  border-radius: 0 0 10px 10px;
}

.accordion-body {
  padding-top: 0;
}

/* for small screens */
@media (max-width: 768px) {
  .accordion-header button {
    font-size: 20px;
  }

  .accordion-body .accordion-header .accordion-button {
    font-size: 12px;
  }
}

              
            
!

JS

              
                const accordionList = [
  {
    parent: "0",
    id: "1",
    title: "Parent 1",
    body: "",
    accordion: [
      {
        parent: "1",
        id: "1-1",
        title: "Child 1",
        body:
          "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>",
        accordion: []
      },
      {
        parent: "1",
        id: "1-2",
        title: "Child 2",
        body:
          "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>",
        accordion: []
      }
    ]
  },
  {
    parent: "0",
    id: "2",
    title: "Parent 2",
    body: "",
    accordion: [
      {
        parent: "2",
        id: "2-1",
        title: "Child 1",
        body:
          "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>",
        accordion: []
      },
      {
        parent: "2",
        id: "2-8",
        title: "Child 2",
        body: "",
        accordion: [
          {
            parent: "1-2",
            id: "1-2-1",
            title: "Sub Child 1",
            body: "",
            accordion: [
              {
                parent: "1-2-1",
                id: "1-3-2",
                title: "Sub Child 1",
                body:
                  "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>",
                accordion: []
              },
              {
                parent: "1-2-1",
                id: "1-3-1",
                title: "Sub Child 2",
                body:
                  "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>",
                accordion: []
              }
            ]
          }
        ]
      }
    ]
  },
  {
    parent: "0",
    id: "3",
    title: "Parent 3",
    body: "",
    accordion: [
      {
        parent: "3",
        id: "3-1",
        title: "Child 1",
        body:
          "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>",
        accordion: []
      },
      {
        parent: "3",
        id: "3-2",
        title: "Child 2",
        body: "",
        accordion: [
          {
            parent: "3-2",
            id: "3-2-1",
            title: "Child 1",
            body: "",
            accordion: [
              {
                parent: "3-2-1",
                id: "3-2-2",
                title: "Sub Child 2",
                body:
                  "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>",
                accordion: []
              },
              {
                parent: "3-2-1",
                id: "3-2-3",
                title: "Sub Child 3",
                body:
                  "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>",
                accordion: []
              }
            ]
          }
        ]
      },
      {
        parent: "3",
        id: "3-3",
        title: "Child 3",
        body:
          "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>",
        accordion: []
      }
    ]
  }
];
// JSON END ( remember to remove the ";" when copying)
let levelsDeep = 0;
let startItemText =
  '<!-- Start Parent Accordion  --> <div class="accordion-item"><h2 class="accordion-header"><button class="accordion-button collapsed ';
let accordionBody = "";

function loopCall(arrayA) {
  for (let i in arrayA) {
    accordionBody += startItemText;
    let bodyID = arrayA[i].id;
    console.log(arrayA[i].body != "");
    if (arrayA[i].body != "") {
      accordionBody += 'hasBody" type="button" data-bs-toggle="collapse" ';
    } else {
      accordionBody += '" type="button" data-bs-toggle="collapse" ';
    }
    ids =
      'data-bs-target="#bodycontainer-' +
      bodyID +
      '" aria-expanded="false" aria-controls="#bodycontainer-' +
      bodyID +
      '"> <!-- Accordion Title -->';

    accordionBody += ids + arrayA[i].title + "</button></h2>";
    accordionBody +=
      '<div id="bodycontainer-' +
      bodyID +
      '" class="accordion-collapse collapse" data-bs-parent="#bodycontainer-' +
      arrayA[i].parent +
      '"><!-- Accordion Parent Body --><div class="accordion-body">';
    if (arrayA[i].accordion != 0) {
      levelsDeep++;
      loopCall(arrayA[i].accordion);
    } else {
      accordionBody += "<div class='onlyBody'>";
      accordionBody += arrayA[i].body;
      accordionBody += "</div></div></div></div><!-- body Wrap --!>";
    }
  }
  accordionBody += "</div></div></div><!-- End Accordion Parent Body -->";
}

loopCall(accordionList);
document.getElementById("bodycontainer-0").innerHTML = accordionBody;

              
            
!
999px

Console