Accordion in Bootstrap 5.3 behaving independently of data-bs-parent

I am currently working on designing an accordion feature where the headers are displayed in a column on the left side, and when expanded, the content appears in a column on the right side. Everything is working perfectly except for the fact that I have to click on a header again to close it, similar to the "Always Open" example on https://getbootstrap.com/docs/5.3/components/accordion/, even though I have populated data-bs-parent.


        <div class="row justify-content-center">
            <!-- BEGIN LEFT SIDE -->
            <div class="col-4">
                <div class="accordion accordion-flush" id="catAccordion">
                    {% for topic in topic_list %}
                        <div class="accordion-item">
                            <h2 class="accordion-header" style="display:flex;">
                                <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse_t{{ topic.id }}" aria-expanded="false" aria-controls="collapse_t{{ topic.id }}">
                                    <h5>{{ topic.title }}</h5>
                                </button>
                            </h2>
                        </div>
                    {% endfor %}
                </div>
            </div>
            <!-- END LEFT SIDE -->

            <!-- BEGIN RIGHT SIDE -->
            <div class="col-6">
                {% for topic in topic_list %}
                    <div id="collapse_t{{ topic.id }}" class="accordion-collapse collapse" data-bs-parent="#catAccordion" aria-labelledby="head_t{{ topic.id }}">
                        <div class="accordion-body">{{ topic.id }} - {{ topic.title }}</div>
                    </div>
                {% endfor %}
            </div>
            <!-- END RIGHT SIDE -->
        </div>
    

Answer №1

To properly implement the accordion feature in Bootstrap, the data-bs-parent element should enclose the accordion-item elements. In your current code, the parent is defined as id="catAccordion", but this div does not contain any accordion items, rendering the Bootstrap functionality ineffective. To resolve this issue, you can simply transfer the id attribute to the outermost containing div, as demonstrated below:

<div class="row justify-content-center" id="catAccordion">
  <div class="col-4">
    <div class="accordion accordion-flush" >
      <div class="accordion-item">
        <h2 class="accordion-header" style="display:flex;">
          <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse_t1">
          <h5>Title 1</h5>
          </button>
        </h2>
      </div>
      <div class="accordion-item">
        <h2 class="accordion-header" style="display:flex;">
          <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse_t2">
          <h5>Title 2</h5>
          </button>
        </h2>
      </div>
      <div class="accordion-item">
        <h2 class="accordion-header" style="display:flex;">
          <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse_t3">
            <h5>Title 3</h5>
            </button>
        </h2>
      </div>
    </div>
  </div>
  <div class="col-6">
    <div id="collapse_t1" class="accordion-collapse collapse" data-bs-parent="#catAccordion">
      <div class="accordion-body">Title 1 Content</div>
    </div>
    <div id="collapse_t2" class="accordion-collapse collapse" data-bs-parent="#catAccordion">
      <div class="accordion-body">Title 2 Content</div>
    </div>
    <div id="collapse_t3" class="accordion-collapse collapse" data-bs-parent="#catAccordion">
      <div class="accordion-body">Title 3 Content</div>
    </div>
  </div>
</div>


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5979a9a818681879485b5c0dbc6dbc7">[email protected]</a>/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="10727f7f64636462716050253e233e22">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

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

Utilize getJSON to refresh the JavaScript datetimepicker

I am currently using an api with ajax (getJSON) to append data to a specific div called 'open' on my website. However, I now want to update the static values in my datetime picker script for minTime and maxTime. Here is the code snippet: AJAX ...

Why does the address bar show value changes in IE when using window.location.hash, but the value doesn't change in the DOM when going

Here is a sample JavaScript code that attempts to detect the back button event: document.location.hash="#1"; document.location.hash="#2"; document.location.hash="#3"; function check() { if("#3"!=window.location.hash) { alert("Back button clic ...

Tips for executing a .exe file in stealth mode using JavaScript?

I am currently working on the transition of my vb.net application to JavaScript and I am facing a challenge. I need to find a way to execute an .exe file in hidden mode using JS. Below is the snippet from my vb.net code: Dim p As Process = New Pro ...

The redirect feature in Next.js 14 is experiencing delays

Having an issue with the redirect function in next.js 14. The problem is that the redirect process on the server takes approximately 5 minutes. I am currently using a VPS on Namecheap with WHM and cPanel, running the application on Apache server with passe ...

Encountering the error message "AppRegistryNotReady: Apps have not been loaded" while attempting to deploy to Heroku with Django

Whenever I try to deploy to production (Heroku), I keep encountering an error, yet the app functions smoothly on my localhost. Despite finding numerous solutions suggested by others with a similar issue, nothing seems to work for me. It's worth noting ...

What is the proper method for invoking object (class) methods from a router?

My apologies for the vague title. Let me clarify what I am attempting to accomplish. In this scenario, there are two main components: A class called 'wallet.js' A router named 'index.js' which handles GET requests This is my objectiv ...

Transitioning from Jquery to vanilla JavaScript or transforming Jquery code into pseudo code

Struggling with a snippet of jQuery code here. While I have a good grasp on JavaScript, my knowledge of jQuery is somewhat limited. I am seeking assistance to analyze the following code in order to translate it into plain JavaScript or pseudo code that ca ...

What is the best way to eliminate the initial key from a JSON object

How can I remove the first index in a JSON object? JSON: {"data":[{"label":"Data","data":1},{"label":"Website","data":1}]} I need: [{"label":"Data","data":1},{"label":"Website","data":1}] When I try to delete .data, it outputs object{} JavaScript c ...

The Bootstrap navigation bar vanishes when viewed on mobile devices

I've integrated Bootstrap 5 with my navbar, but when viewed on a mobile device, the buttons disappear. Even hovering over them doesn't display anything. Is there a solution for this issue? Below is the code snippet: <nav class="navbar na ...

How to vertically align the elements within a DIV in jQuery Mobile

Is there a way to vertically align the content of a jQuery grid's block so that it is centered with another element in the same row? <div class="ui-grid-a" style="border:1px solid"> <div class="ui-block-a" style="vertical-align:middle" >& ...

I encountered an issue when trying to dynamically add a text field in Angular 2. The error message received was "ERROR TypeError: Cannot read property '0' of

I am currently utilizing Angular2 in my project, and I am attempting to dynamically add a text field. However, I keep encountering an error: Error Message (TS): ngOnInit() { this.myForm = this._fb.group({ myArray: this._fb.array([ ...

How come the translateX function doesn't function properly for fixed elements in IE9, IE10, and IE11?

Presently, I am endeavoring to obtain the desired outcome in Internet Explorer versions 9, 10, and 11 (functions flawlessly on Chrome and Firefox): When in mobile mode, there is a principal #container envelop that encompasses the entire website content al ...

I am having trouble displaying images with Material UI CardMedia

I'm currently using CardMedia to display images on my webpage, but unfortunately, the image is not appearing as expected. After researching a similar issue on Stack Overflow, I discovered that the suggested solution involved importing the image and t ...

how can I include an AngularJS variable as a parameter in an onclick function?

I attempted to utilize an AngularJS variable as an argument value inside onclick() in order to invoke a JavaScript function. Can someone provide me with instructions on how to achieve this? Here is my code: <div onclick="deleteArrival({{filterList.id} ...

What causes the React Query cache to be cleared upon page reload?

Hi there, I am new to Next.js and React Query. I would really appreciate any help or advice. I apologize for any mistakes in my English language skills. Currently, I am using Next.js v12 and React Query v3, along with the React Query DevTools. On one of ...

Managing Asynchronous Callbacks in JavaScript using Node.js

I am a beginner in the world of Javascript and I recently encountered a challenge with async callbacks using Node.js. My first step was setting up the Facebook webhook and sending a Webhook POST request Below is the code snippet : routes.js **Setting up ...

When selecting the "Open Link in New Tab" option in Chrome, the Angular app's routing will automatically redirect to the login page

I am facing a peculiar issue in my Angular 2 application that I need help troubleshooting. Currently, the routing within my app functions as intended when I click on links to navigate between different components. Here is an example of how the routing path ...

What is the best way to execute a function to compare two dates based on their respective months?

I am currently working on a function to compare the current month with the month input provided by a user. The user will input dates in the format 'YYYY-MM-DD'. Below is my initial code snippet: class Sneaker(models.Model): name = models.CharFie ...

Leverage and repurpose OOP objects

I am exploring how to inherit from Button using prototypes. Despite my efforts, the alerted name remains "Sarah" as it is the last Child created. I believe the Creator Class should be responsible for setting the name using a Method in Button. Check out m ...

What is the purpose of storing JSON data as a string within another JSON object?

Screenshot of developer tools While exploring the local storage on my visit to YouTube, I came across some very peculiar-looking JSON data. { creation: 1638112285935, data: "{\"quality\":1080,\"previousQuality&bs ...