'Listening to the Bootstrap collapse shown/hidden events?
I have a Bootstrap 5 accordion that looks like this:
<div class="accordion" id="my_accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading_bar">
<button class="accordion-button collapsed bg-light my-collapse" type="button" data-bs-toggle="collapse" data-bs-target="#collapse_bar" aria-expanded="false" aria-controls="collapse_bar">Bar</button>
</h2>
<div id="collapse_bar" class="accordion-collapse collapse" aria-labelledby="heading_bar" data-bs-parent="#my_accordion">
<div class="accordion-body">
...
</div>
</div>
</div>
</div>
I am trying to listen to the shown
and hidden
events like described in the collapse docs:
return $('button.my-collapse[data-bs-toggle="collapse"]').on('shown.bs.collapse', function(e) {
console.log("This is not fired...")
});
But for some reason the events do not fire. I know that I have the right selector, because if I change shown.bs.collapse
to click
an event occurs.
Any idea what's happening?
Btw, my logic also seems to work for tabs like this:
<div class="card">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs" id="debug_tab" role="tablist">
<li class="nav-item"><a class="nav-link my-tab" id="products-tab" data-bs-toggle="tab" href="#foo" role="tab" aria-controls="foo" aria-selected="false">Foo</a></li>
</ul>
</div>
<div class="card-body">
...
</div>
</div>
when I listen like this:
return $('a.my-tab[data-bs-toggle="tab"]').on('shown.bs.tab', function(e) {
console.log("This is fired...")
});
Solution 1:[1]
Try this:
var myCollapsible = document.getElementById('collapse_bar')
myCollapsible.addEventListener('hidden.bs.collapse', function () {
console.log("Hidden")
});
myCollapsible.addEventListener('shown.bs.collapse', function () {
console.log("Shown")
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
<div class="accordion" id="my_accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading_bar">
<button class="accordion-button collapsed bg-light my-collapse" type="button" data-bs-toggle="collapse" data-bs-target="#collapse_bar" aria-expanded="false" aria-controls="collapse_bar">Bar</button>
</h2>
<div id="collapse_bar" class="accordion-collapse collapse" aria-labelledby="heading_bar" data-bs-parent="#my_accordion">
<div class="accordion-body">
...
</div>
</div>
</div>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kevin M. Mansour |