If I copy this code twice in a page for a Bootstrap collapsible, one toggle button will affect other toggles, causing everything to expand or collapse.
How would I resolve this issue? Each collapsible should only affects its own set.
The end goal is to have a collapsible as a shared component. So if I copy it 3,4,5, 100 times in a page, other ones should not be affected. (will be using C# extension methods later).
https://getbootstrap.com/docs/4.0/components/collapse/
Please copy code twice in a page.
<!--Accordion wrapper-->
<div class="accordion md-accordion" id="accordionEx" role="tablist" aria-multiselectable="true">
<!-- Accordion card -->
<div class="card">
<!-- Card header -->
<div class="card-header" role="tab" id="headingOne1">
<a data-toggle="collapse" data-parent="#accordionEx" href="#collapseOne1" aria-expanded="true"
aria-controls="collapseOne1">
<h5 class="mb-0">
Collapsible Group Item #1 <i class="fas fa-angle-down rotate-icon"></i>
</h5>
</a>
</div>
<!-- Card body -->
<div id="collapseOne1" class="collapse show" role="tabpanel" aria-labelledby="headingOne1"
data-parent="#accordionEx">
<div class="card-body">
1111 test
</div>
</div>
</div>
<!-- Accordion card -->
<!-- Accordion card -->
<div class="card">
<!-- Card header -->
<div class="card-header" role="tab" id="headingTwo2">
<a class="collapsed" data-toggle="collapse" data-parent="#accordionEx" href="#collapseTwo2"
aria-expanded="false" aria-controls="collapseTwo2">
<h5 class="mb-0">
Collapsible Group Item #2 <i class="fas fa-angle-down rotate-icon"></i>
</h5>
</a>
</div>
<!-- Card body -->
<div id="collapseTwo2" class="collapse" role="tabpanel" aria-labelledby="headingTwo2"
data-parent="#accordionEx">
<div class="card-body">
2222 test
</div>
</div>
</div>
<!-- Accordion card -->
<!-- Accordion card -->
<div class="card">
<!-- Card header -->
<div class="card-header" role="tab" id="headingThree3">
<a class="collapsed" data-toggle="collapse" data-parent="#accordionEx" href="#collapseThree3"
aria-expanded="false" aria-controls="collapseThree3">
<h5 class="mb-0">
Collapsible Group Item #3 <i class="fas fa-angle-down rotate-icon"></i>
</h5>
</a>
</div>
<!-- Card body -->
<div id="collapseThree3" class="collapse" role="tabpanel" aria-labelledby="headingThree3"
data-parent="#accordionEx">
<div class="card-body">
3333 test
</div>
</div>
</div>
<!-- Accordion card -->
</div>
Looking to isolate the behavior. Would like to avoid javascript route, but if needed thats fine. Maybe something to do with everyone sharing same Id, data-parents, etc.
When using Bootstrap's Accordion, especially when you are just copying the cards, don't forget to check that the id of the card body MUST be unique and the href of the card header must match it.
WRONG (both card body have the same id, which is collapseOne):
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="headingOne">
<h2 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Collapsible Group Item #1
</button>
</h2>
</div>
<div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
Body of Collapsible #1
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h2 class="mb-0">
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Collapsible Group Item #2
</button>
</h2>
</div>
<div id="collapseOne" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
Body of Collapsible #2
</div>
</div>
</div>
</div>
CORRECT (each card body has its own id):
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="headingOne">
<h2 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Collapsible Group Item #1
</button>
</h2>
</div>
<div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
Body of Collapsible #1
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h2 class="mb-0">
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Collapsible Group Item #2
</button>
</h2>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
Body of Collapsible #2
</div>
</div>
</div>
</div>
Related
I have an accordion inside a modal, but the buttons doesn't fit into their item banner. There is a GIF to explain what I am talking about. Thanks in advance!
Accordion button to fit in its banner.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<div class="modal fade show d-block" id="ot" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="ott">Other Scripts</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div>
<div class="modal-body">
<i> To close this page, click the X button, or the "Close" button to close. Clicking off the pop-up will not work. </i>
<h2> Other Scripts </h2>
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
Nitro Generator
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse" aria-labelledby="headingOne" data-bs-parent="#accordionExample" style="">
<div class="accordion-body">
<h4> Disclaimer </h4>
<p> Note that an actual working key would take over a billion years to find. </p>
<br>
<p> Nitro Generator (Python) Nitro Verify (Python) by nasus</p>
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingTwo">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Kahoot Scripts
</button>
</h2>
<div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
<div class="accordion-body">
<p> Kahoot Answers (Python) Kahoot Lobby Flood (Python | KahootPY | Threading) by Nasus</p>
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingThree">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Other Scripts
</button>
</h2>
<div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
<div class="accordion-body">
<p> User Agents (TXT)
Invite Generator and Checker (Python)
Webhook Deleter (Python)
TikTok Username Checker (Python)
Bot's Code (Python) by Nasus</p>
<p>
Base64 (Python)
Windows Screen Grabber V1 (Python)
Windows Screen Grabber V2 (Python) by 𝙂𝙝𝙤𝙨𝙩.𝙄𝙣𝙏𝙝𝙚.𝙎𝙝𝙚𝙡𝙡</p>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button></div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
I'm trying to design a blog with 3 cards in a card group then I add collapse buttons to the bottom of every card and the problem is when I press any button they are all pressed together and show the first collapse button's text even though I added bootstrap css link and the javascript two links, I'm using bootstrap and here is my code's body , any suggestions ?
<body>
<div class="container">
<div class="card-group m-2">
<div class="card">
<img src="https://assets.codepen.io/6093409/mountains-1.jpg" alt="a snow-capped mountain range"/>
<div class="card-body">
<h2 class="card-title">Mountains</h2>
<p class="card-text">This is a photo of snowy-covered mountains. How majestic.</p>
<p>
<a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
Link with href
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Button with data-bs-target
</button>
</p>
<div class="collapse" id="collapseExample">
<div class="card card-body">
Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
</div>
<div class="card">
<img src="https://assets.codepen.io/6093409/mountains-4.jpg" alt="a snowy mountain with clouds behind it"/>
<div class="card-body">
<h2 class="card-title">Mountains</h2>
<p class="card-text">This is a photo of snowy-covered mountains. How majestic.</p>
<p>
<a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
Link with href
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Button with data-bs-target
</button>
</p>
<div class="collapse" id="collapseExample">
<div class="card card-body">
Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
</div>
<div class="card">
<img src="https://assets.codepen.io/6093409/mountains-3.jpg" alt="a mountain range under a thin layer of clouds"/>
<div class="card-body">
<h2 class="card-title">Mountains</h2>
<p class="card-text">This is a photo of snowy-covered mountains. How majestic.</p>
Learn more
</div>
</div>
</div>
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/#popperjs/core#2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>
you are using the same id selector for both, that is why a click on one button will affect the other, to fix the problem, use a separate #id selector,
see below for a solution:
<body>
<div class="container">
<div class="card-group m-2">
<div class="card">
<img src="https://assets.codepen.io/6093409/mountains-1.jpg" alt="a snow-capped mountain range"/>
<div class="card-body">
<h2 class="card-title">Mountains</h2>
<p class="card-text">
This is a photo of snowy-covered mountains. How majestic.
</p>
<p>
<a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample1" role="button" aria-expanded="false" aria-controls="collapseExample">
Link with href
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample1" aria-expanded="false" aria-controls="collapseExample">
Button with data-bs-target
</button>
</p>
<div class="collapse" id="collapseExample1">
<div class="card card-body">
Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
</div>
<div class="card">
<img src="https://assets.codepen.io/6093409/mountains-4.jpg" alt="a snowy mountain with clouds behind it"/>
<div class="card-body">
<h2 class="card-title">Mountains</h2>
<p class="card-text">This is a photo of snowy-covered mountains. How majestic.</p>
<p>
<a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample2" role="button" aria-expanded="false" aria-controls="collapseExample">
Link with href
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample2" aria-expanded="false" aria-controls="collapseExample">
Button with data-bs-target
</button>
</p>
<div class="collapse" id="collapseExample2">
<div class="card card-body">
Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
</div>
<div class="card">
<img src="https://assets.codepen.io/6093409/mountains-3.jpg" alt="a mountain range under a thin layer of clouds"/>
<div class="card-body">
<h2 class="card-title">Mountains</h2>
<p class="card-text">This is a photo of snowy-covered mountains. How majestic.</p>
Learn more
</div>
</div>
</div>
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/#popperjs/core#2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>
I am new to the Django template language and I am playing around with bootstrap collapses to display my items. Below is my code:
{% for item in items %}
<div id="accordion">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
{{item.item_name}}
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
{{item.description}}
</div>
</div>
</div>
</div>
{% endfor %}
This is taken from the Bootstrap documentation for collapse, here. In this implementation, all of the items passed in would have the same id, collapseOne. So whenever I click on an item, any item, it would collapse/uncollapse every item on the page. I've tried id="collapse{{item.item_name}}" but that doesn't seem to work. Is there a way to pass in variables in <div> id?
try this
{% for item in items %}
<div id="accordion">
<div class="card">
<div class="card-header" id="heading{{forloop.counter}}">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapse{{forloop.counter}}" aria-expanded="true" aria-controls="collapse{{forloop.counter}}">
{{item.item_name}}
</button>
</h5>
</div>
<div id="collapse{{forloop.counter}}" class="collapse show" aria-labelledby="heading{{forloop.counter}}" data-parent="#accordion">
<div class="card-body">
{{item.description}}
</div>
</div>
</div>
</div>
{% endfor %}
https://docs.djangoproject.com/en/3.1/topics/templates/#the-django-template-language
I found that somebody else asked the same question and received no answer. I am using Bootstraps data-toggle for an accordion and it is not working on mobile (not just IOS but it's not working on chrome developer tools or my Galaxy S8).
<!-- How it Works -->
<div id="howItWorks" class="interiorContent">
<div class="container">
<div class="intContHead">
<h2>How it Works</h2>
</div>
<div class="accordion" id="howItWorksAccordion">
<div class="card">
<div class="card-header" id="headingOne">
<h6 class="mb-0">
<a role="button" data-toggle="collapse" data-parent="#howItWorksAccordion" data-target="#collapseOne" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Step 1: Create ASComm.NET Configuration File
</a>
</h6>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#howItWorksAccordion">
<div class="card-body">
<p>For more advanced applications (branded, database, etc.), you can build your own configuration
application that utilizes ASComm.NET's configuration and serialization capabilities.</p>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h6 class="mb-0">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#howItWorksAccordion" href="#collapseTwo" aria-expanded="false"
aria-controls="collapseTwo">
Step 2: Add ASComm Excel Add-in to Excel.
</a>
</h6>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#howItWorksAccordion">
<div class="card-body">
<p>
<strong>NOTE: Step 2 only needs to be perfomed once. If you have already added ASComm.NET Excel Add-in
to Excel, proceed to Step 3.</strong>
</p>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingThree">
<h6 class="mb-0">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#howItWorksAccordion" href="#collapseThree" aria-expanded="false"
aria-controls="collapseThree">
Step 3: Configure ASComm.NET Excel Add-in Operating Parameters
</a>
</h6>
</div>
<div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#howItWorksAccordion">
<div class="card-body">
<p>3.1 Locate the ASComm.NET tab on the Excel ribbon</p>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingFour">
<h6 class="mb-0">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#howItWorksAccordion" href="#collapseFour" aria-expanded="false"
aria-controls="collapseFour">
How to Read and Log Data
</a>
</h6>
</div>
<div id="collapseFour" class="collapse" aria-labelledby="headingFour" data-parent="#howItWorksAccordion">
<div class="card-body">
<img alt="Excel AddIn Ribbon" class="img-responsive" src="../../../images/ascomm_excel_addin_ribbon.png">
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingFive">
<h6 class="mb-0">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#howItWorksAccordion" href="#collapseFive" aria-expanded="false"
aria-controls="collapseFive">
How to Write Data
</a>
</h6>
</div>
<div id="collapseFive" class="collapse" aria-labelledby="headingFive" data-parent="#howItWorksAccordion">
<div class="card-body">
<!--Manual Start-->
<p>ASComm Excel Add-in can read data from hardware devices and populate Excel spreadsheets without
writing any code.</p>
</div>
</div>
</div>
</div>
</div>
</div>
I tried adding curser: pointer to my a tags and I tried changing the a tags to button tags. I watched the elements in chrome developer tools as I clicked on the links - the first card changes from class="collapse show" to class="collapsing" to class="collapse" yet nothing happens. (The first card is supposed to be shown upon page visit).
Pasted your code in a ongoing project file...uploaded on web server...EVERYTHING WORKED FINE... Tested on Chrome Mobile Browser... version 68.0 ...
Versions I was using (If it helps):
Bootstrap 4.1.2
JQuery 3.3.1
Popper.js 1.14.3
I have a bootstrap accordion like this in my rails app.
<div class="accordion" id="accordion">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
Collapsible Group Item #1
</a>
</div>
<div id="collapseOne" class="accordion-body collapse" style="height: 0px; ">
<div class="accordion-inner">
s1
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
Collapsible Group Item #2
</a>
</div>
<div id="collapseTwo" class="accordion-body collapse" style="height: 0px; ">
<div class="accordion-inner">
s2
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
Collapsible Group Item #3
</a>
</div>
<div id="collapseThree" class="accordion-body in collapse">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12"><button type="button" class="btn btn-primary" id="proceed_to_order">Proceed</button></div>
</div>
Here's the fiddle.
So when I click, I am carrying these values to the next page (which I am unable to put in the fiddle). So In Mozila browser when I click back in the browser the last accordion when I had opened and clicked on proceed remains open. But for Chrome when I click back, all the accordion is automatically are closed which I don't want.
Is there any work around for this?
Please suggest.
replace with:
<div id="collapseThree" class="accordion-body collapse" style="height: 0px; ">
<div class="accordion-inner">
s3
</div>
</div>
in third number of accordion group..