Accordion nested within another accordion

I am facing a challenge while trying to nest an accordion within another accordion. The issue is that the nested accordion only expands as much as the first accordion, requiring additional space to display its content properly. Any assistance with resolving this problem would be greatly appreciated!

Custom CSS

.accordion-button {
  background-color: #73560b;
  border: 2px solid #ffc600;
  border-radius: 0px 10px 0px 10px;
  box-shadow: 7px 7px 5px #cccccc;
  color: #fff;
  cursor: pointer;
  padding: 10px 15px 10px 15px;
  margin: 4px 0px 7px 0px;
  width: 100%;
  font-size: 18px;
  transition: 0.4s;
  outline: none;
  text-align: left;
}
  
.accordion-button.active, .accordion-button:hover {
  background-color: #926c0e;
}

.accordion-button:after {
  content: '\002B';
  color: #ffc600;
  font-weight: bold;
  float: right;
}

.accordion-button.active:after {
  content: "\2212";
}

.panel-content {
  padding: 0 18px;
  background-color: white;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}

HTML Structure

<button class="accordion-button">Main Section</button>
<div class="panel-content">
Content for main section
<button class="accordion-button">Subsection 1</button>
<div class="panel-content">
Content for subsection 1
</div>
</div>

JavaScript Code

var accordionButtons = document.getElementsByClassName("accordion-button");
var j;

for (j = 0; j < accordionButtons.length; j++) {
  accordionButtons[j].onclick = function() {
    this.classList.toggle("active");
    var panelContent = this.nextElementSibling;
    if (panelContent.style.maxHeight){
      panelContent.style.maxHeight = null;
    } else {
      panelContent.style.maxHeight = panelContent.scrollHeight + "px";
    } 
  }
}

Answer №1

let accordions = document.getElementsByClassName("accordion");
let counter;

for (counter = 0; counter < accordions.length; counter++) {
    accordions[counter].onclick = function(){
        this.classList.toggle("active");
        let tab = this.nextElementSibling;
        if (tab.style.display === "block") {
            tab.style.display = "none";
        } else {
            tab.style.display = "block";
        }
    }
}
button.accordion {
    background-color: #f4f4f4;
    color: #333;
    cursor: pointer;
    padding: 20px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 16px;
    transition: 0.4s;
}

button.accordion.active, button.accordion:hover {
    background-color: #e0e0e0; 
}

div.panel {
    padding: 0 20px;
    display: none;
    background-color: #fff;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<h2>Accordion Widget</h2>



<button class="accordion">Section 2</button>
<div class="panel">
  <button class="accordion">Section 1</button>
  <div class="panel">
    <p>
      Your content goes here
    </p>
  </div>
</div>

<button class="accordion">Section 3</button>
<div class="panel">
  <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.</p>
</div>


</body>
</html>

give it a go!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on ensuring that the Angular frontend waits for the response from an Express REST call

Upon initializing my user Component, I want to trigger a REST-Call so that the user profile is displayed when the page loads. The process works smoothly as the component communicates with the service, which in turn contacts my express backend to make the n ...

There seems to be an issue as the function reporter.beforeLaunch cannot be identified and

I am currently attempting to incorporate the protractor screenshot reporter for jasmine 2. However, I encountered the following error in the terminal: /usr/local/bin/node lib/cli.js example/conf.js /Users/sadiq/node_modules/protractor/node_modules/q/q.j ...

Problem with spacing in a Bootstrap 5 column of size medium-5

I'm currently working on a template for my website and facing an issue with removing the margin displayed on the right side of my div using flex. I've tried setting the margin to 0 on one of the columns and also experimented with changing the col ...

How can I implement a feature to automatically close a drawer in a React application using the Material UI

Currently, I am utilizing a React material design theme However, I am facing an issue where the drawer navigation button on mobile view does not close automatically upon clicking I need something like (onClick={handleClose}) to resolve this problem Does ...

Hide the search popup when the user clicks on the "find" button within the jqgrid

What is the solution to closing the search popup when clicking on the Find button? When I search for data in jqgrid and click on the Find button, the Search popup remains open even though the data has been filtered. How can I close the popup after searchin ...

Updating a field in Mongoose by referencing an item from another field that is an array

I have developed an innovative Expense Tracker Application, where users can conveniently manage their expenses through a User Collection containing fields such as Name, Amount, Expenses Array, Incomes Array, and more. The application's database is p ...

Content will be loaded only after the user clicks on the item

On my blog, I have a share button that displays different social media platforms when clicked. However, the page takes longer to load due to fetching data from multiple servers like Facebook and Twitter. Is there a way to use jQuery to make these buttons l ...

Angular & Loopback: The function User.login is not recognized

Today, I encountered an error while attempting to execute the Login function in Ionic. An error message popped up stating: TypeError: User.login is not a function (found in controller.js). Here's a snippet from my controller.js : angular.module(&ap ...

Using jQuery to create a dynamically moving div that forms an endless wall

In order to create a continuous wall that pulls text from a database and displays it, I've developed the following code: $(function() { var x = 0; var y = 100.0; var z=0; setInterval(function() { x -= 0.1; y -= 0.1; ...

Issues with Bootstrap Carousel Fade Transition and Button Controls inoperable

I am currently exploring the creation of a carousel on bootstrap that transitions smoothly between three images (specifically 3 'gorilla' jpgs). However, I am encountering some challenges while learning HTML. Challenge 1: The carousel is not tra ...

The process of passing $refs in Vue explained

I have a feature where all the data is passed to the child component. Currently, I am able to pass $attrs and $listeners successfully: <template> <el-form v-on="$listeners" v-bind="$attrs" :label-position="labelPosition"> <slot /> ...

Issues with the directory for Chrome

Currently, I am utilizing jQuery and running an HTML file on my local machine without a server. Interestingly, the code works perfectly fine on Firefox but encounters issues on Chrome: $('#result').load('test.html'); It appears that t ...

IE/Firefox returning empty value for Ajax requests

After spending two days trying to solve the issue, I still can't figure out why this code is failing in IE (v11) and Firefox while working fine in Chrome. I have read many questions on similar topics involving caching problems with multiple Ajax call ...

WARNING: No matches found in the MySQL database for the searched word

Exploring the realm of coding 1) Gathering data from MySQL via PHP 2) Transferring data from PHP to D3 based on input using a PHP URL. Want to trigger an alert when the text in the input field does not match any records in the MySQL database. Upon testi ...

The functionality for bold and italics does not seem to be functioning properly in either Firefox

Text formatted with <b></b> and <i></i> tags appears correctly on iPhone and Internet Explorer, but lacks formatting in Firefox or Chrome. I have included the .css files below. Additionally, I attempted to add i { font-style:italic ...

What might be the reason behind Chrome occasionally not updating the page when I resize the browser window?

Currently, I am in the process of developing a responsive design site prototype. Everything is going smoothly except for one peculiar issue that only seems to occur in Chrome. Sometimes, when expanding the window, the browser appears to get stuck between s ...

JavaScript Function to Redirect Page After a Delay of X Seconds

I'm trying to implement a redirect to a specific URL after displaying an error message for 5 seconds. Initially, I used JavaScript like this: document.ready(window.setTimeout(location.href = "https://www.google.co.in",5000)); However, the redirectio ...

Guide to locating a particular node within an array of nested objects by utilizing the object

Dealing with an array of nested objects, the goal is to compare values with a flat array and update the property matchFound. If the parent's matchFound is true, then all its children should inherit this value. treeData = [{ field: 'make&a ...

Tips for arranging Radio buttons in multiple columns within the same Radio group in Material UI

As a newcomer in the world of React.js, I am feeling a bit anxious about posting my question on Stack Overflow. I hope everyone can overlook my lack of experience and forgive me for any mistakes. I have been struggling to find the right solution to my prob ...

What is the process of connecting marionette rendered views to the DOM?

I am encountering a challenge with my simple setup using Marionette that I am trying to resolve. My issue lies within a view containing a collection: var MyItemsView = Marionette.View.extend({ template: "#some-template" }); var view = new MyItemsVie ...