I have 4 bootstrap accordions all collapsed. When clicking from navigation link ([href="#headingTwo"] etc...] I need to expand that referenced accordion only. [ AND close it if clicked on another navigation link ]
So the below code works but I'm repeating it 4 times.... Surely it can be simplified into one function?
// accordion 1
$('a.js-scroll-trigger[href="#headingTwo"]').on('click', function() {
document.getElementById("collapseTwo").classList.add("show");
});
// accordion 2
$('a.js-scroll-trigger[href="#headingThree"]').on('click', function() {
document.getElementById("collapseThree").classList.add("show");
});
// accordion 3
$[...]
// accordion 4
$[...]
Many thanks in advance.
CodePudding user response:
It looks like your anchor share the same class .js-scroll-trigger
, you could assign the click handler to all of them, within the handler you will get the target accordion item from the anchor's href
, that it's the id
of the accordion item, then you can just trigger the click
handler of the item button like below.
href
contains the accordion header id
, the element contains as it's first child a button
that triggers the expand/collapse
<h2 id="headingOne">
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
$(function() {
$("a.js-scroll-trigger").on("click", function() {
var target = $(this).attr("href");
$(target).find("button").trigger("click");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.3/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<a href="#headingOne" >Item 1</a>
<a href="#headingTwo" >Item 2</a>
<a href="#headingThree" >Item 3</a>
</div>
<br>
<div id="accordionExample">
<div >
<h2 id="headingOne">
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
<div id="collapseOne" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
<div >
<strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and
hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div >
<h2 id="headingTwo">
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Accordion Item #2
</button>
</h2>
<div id="collapseTwo" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
<div >
<strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing
and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div >
<h2 id="headingThree">
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Accordion Item #3
</button>
</h2>
<div id="collapseThree" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
<div >
<strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and
hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
</div>