``I'm experiencing an issue with the Bootstrap 4 accordion where it is only functioning properly for the

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js"></script>
<div id="accordion">
  <div class="card">
    <div class="card-header">
      <a class="card-link" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                    Collapsible Group Item #1
                </a>
    </div>
    <div id="collapseOne" class="collapse show">
      <div class="card-body">
        Unique Lorem text..
      </div>
    </div>
  </div>
  <div class="card">
    <div class="card-header">
      <a class="collapsed card-link" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                    Collapsible Group Item #2
                </a>
    </div>
    <div id="collapseTwo" class="collapse">
      <div class="card-body">
        Unique Lorem text..
      </div>
    </div>
  </div>
  <div class="card">
    <div class="card-header">
      <a class="collapsed card-link" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
                    Collapsible Group Item #3
                </a>
    </div>
    <div id="collapseThree" class="collapse">
      <div class="card-body">
        Unique Lorem text..
      </div>
    </div>
  </div>
</div>

this accordion code not functioning as expected in my HTML project. I have made several attempts to make it work but it has been unsuccessful.

I even tried the W3Schools snippet provided above. The first one works perfectly, but when I try to collapse the second one, it doesn't work properly. It collapses for a moment and then automatically disappears.

I also attempted to activate the collapse feature using jQuery, but encountered the same issue.

Answer №1

Check out this working fiddle I made for you

It seems like you might have overlooked including jQuery before the bootstrap script.

Make sure your imports are structured like this:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f4ebf4f4e1f6aaeef7c4b5aab5b2aab4">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

Remember to prioritize the jQuery import above all others

Answer №2

Great job, your code is functioning properly.

Make sure you have all the necessary files for Bootstrap 4 and jQuery included in your project if needed.

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>


<div id="accordion">
    <div class="card">
        <div class="card-header">
            <a class="card-link" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                Collapsible Group Item #1 Works
            </a>
        </div> 
        <div id="collapseOne" class="collapse show">
            <div class="card-body">
                Lorem ipsum..
            </div>
        </div>
    </div>
    <div class="card">
        <div class="card-header">
            <a class="collapsed card-link" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                Collapsible Group Item #2
            </a>
        </div>
        <div id="collapseTwo" class="collapse">
            <div class="card-body">
                Lorem ipsum..
            </div>
        </div>
    </div>
    <div class="card">
        <div class="card-header">
            <a class="collapsed card-link" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
                Collapsible Group Item #3
            </a>
        </div>
        <div id="collapseThree" class="collapse">
            <div class="card-body">
                Lorem ipsum..
            </div>
        </div>
    </div>
</div>

Check out the demo

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

Avoid using the AJAX function if an identical request is already in progress

I have set up an AJAX request within a JavaScript function. By using the setInterval method, this AJAX function runs every 5000 milliseconds. I am curious if there is a way to determine if the previous AJAX call is still in progress to prevent multiple si ...

Building a form within an Angular Material table component

I am currently facing an issue where I need to embed a form within a mat-table that is contained inside a stepper which is housed within a mat-dialog. Currently, I have set up a table with mat-form-field and mat-input inside the td tags, but my input appea ...

Retrieve the id of the anchor tag that was selected upon clicking, and then dynamically change the content of another div

Seeking guidance on how to dynamically change the content of a div element based on which anchor tag is clicked. Below is the HTML structure and JavaScript function I have attempted: HTML: <div> <a id="a1" href="javascript: changeDiv();">tag1 ...

What is the best way to input individual students' CA and exam scores into distinct fields and then calculate their total scores in a separate text field?

As a beginner in jQuery, I am looking for guidance on creating a script that calculates the Total score, Grade, Remark, and Position based on the user input of CAT score and EXAM score. The result should be displayed dynamically once both scores are entere ...

What is the best way to align the items in two lists at the center as if they were all individual

Q: How can I center the list-item elements of two unordered lists as if they were children of just one list? Example: {[1][1][1][1][2][2]} { [2][2] } CSS Alignment Challenge <div> <ul> &l ...

Creating Dynamic Visibility in ASP.NET Server Controls: Displaying or hiding a textbox based on a dropdown selection

Is there a way to dynamically show/hide a textbox or an entire div section based on a user's selection from a dropdown menu? Can this be achieved using client-side HTML controls instead of server controls? Would utilizing jQuery provide the best solut ...

Quickly switch between pages as they load

In Chrome, I'm experiencing a white flash between page loads that is causing transitions to appear choppy. I've taken various steps to optimize the site, such as using image sprites, reducing image sizes, minifying CSS, ensuring correct CSS loadi ...

Error: The token < was not expected in ng2 bootstrap code

Having encountered the error Unexpected token < while using ng2 bootstrap, I included 'ng2-bootstrap': 'node_modules/ng2-bootstrap' in the configuration file below. System.config.ts (function (global) { System.config({ ...

How many elements were dynamically added to the <div>?

I am using jQuery to dynamically insert images into a div element. After successfully adding an image, I have included an alert(). Is there a way in jQuery to verify if a series of elements has been added to a div tag? ...

Utilizing props for padding and margin customization in React-Bootstrap elements

While working with React-Bootstrap, I have been attempting to apply margins and paddings using props. After going through the documentation provided here, I couldn't find any mention of adding padding or margin, unlike how it is detailed in the offic ...

What is the best way to add a key and value into a specific position within a key-value array using jQuery or JavaScript?

Seeking assistance with jQuery - I have a key value array object and need help var myArray= {}; myArray['key1'] = 'value1'; myArray['key2'] = 'value2'; myArray['key3'] = 'value3'; ...

Tips for fixing the issue of disappearing top margins in elements while using CSS3 Pie in Internet Explorer 7

Hey there! I've been experimenting with a lot of CSS3 effects, but I'm running into issues trying to get the same effects to work on IE 7 and 8. I've been attempting to use CSS3 Pie to help with this. While CSS3 Pie has worked well for som ...

Learn the process of concealing previous markers on a Google Map in JavaScript API when new markers are added

I have implemented a store locator feature using the Google Maps JavaScript API. When users enter their location, they can see all stores within a 50km radius. I am looking to remove the previous marker when a user performs a new search. Currently, there a ...

Display with organized information, Arrange with unprocessed data in DataTables.net

Below is a snippet of the datatables configuration I am working with: { "dom" : "rltip", "processing" : true, "serverSide" : false, "order" : [ [ 1 , "desc" ] ], "searching" : false, data: [ { "column-a" : "Samp ...

Creating dynamic elements with FormData

I'm experiencing an issue with sending files via xhr from a dynamically created element. Here's the code snippet in question: $('#thread_file').prepend('<form action="/nti/'+$time+'" enctype="multipart/for ...

Utilize ASP to limit the application of a CSS file exclusively to the child page

My website consists of a master page and several child pages. I have created specific CSS classes for styling. However, I only want certain child pages to utilize these styles while the master page should always use them. How can I limit the access to th ...

Angular Bootstrapped Navbar with Tab-style Links

I have been working on a project with a specific example: Routing example In my app.component.html file, I made a modification from: <ul class="nav nav-tabs"> to <ul class="nav nav-pills"> Now, the tabs have the appearance of pills. Next ...

Tool to track character limits in multiple text fields

I'm facing a challenge with a form that includes 3 text areas, a copy button, and a reset button. My goal is to concatenate all the characters in the text areas to get a sum which will be displayed next to the copy/reset button. The character limit is ...

Angular directives do not recognize HTML tags as elements but rather treat them as plain text

Let's consider this scenario: There is a function called foo() in my controller: $scope.getOffers = function(){ var txt1 = "aaaa" + "<br>" + "bbbb"; $scope.newData = txt1; }; Here is the relevant HTML snippet: <div class="help-b ...

JavaScript - Placing Image Caption and Details within a Box

<div id="CollectionALL"> <div id="collection1" class="col"> <img id="Img1" class="imageCS"/> <H1 id="Title1"></H1> ...