In the bootstrap docs, there is an example of how to toggle single and multiple targets, see https://getbootstrap.com/docs/4.0/components/collapse/?#multiple-targets.
When clicking “Toggle the first element” and then “Toggle both elements”, the state of the two elements are mismatched e.g. one collapsed the other not.
How can I achieve that both/multiple elements are collapsed/shown when clicking “Toggle both elements” no matter what state the element had before.
Advertisement
Answer
So after Dealish Teppes hint, I came up with this solution. Note to remove the data-target and add id on the “toggle both” button.
JavaScript
x
10
10
1
var toggle_all = $('#toggle_all')
2
toggle_all.on('click', function(event) {
3
if (toggle_all.attr("aria-expanded") === 'false') {
4
$('.multi-collapse').collapse('show')
5
toggle_all.attr('aria-expanded', 'true');
6
} else {
7
$('.multi-collapse').collapse('hide')
8
toggle_all.attr('aria-expanded', 'false');
9
}
10
});
JavaScript
1
46
46
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
2
<!doctype html>
3
<html lang="en">
4
5
<head>
6
<!-- Required meta tags -->
7
<meta charset="utf-8">
8
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
9
10
<!-- Bootstrap CSS -->
11
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
12
13
<title>Bootstrap Card Example</title>
14
</head>
15
16
<body>
17
<p>
18
<a class="btn btn-primary" data-toggle="collapse" href="#multiCollapseExample1" role="button" aria-expanded="false" aria-controls="multiCollapseExample1">Toggle first element</a>
19
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Toggle second element</button>
20
<button id="toggle_all" class="btn btn-primary" type="button" data-toggle="collapse" aria-expanded="false" aria-controls="multiCollapseExample1 multiCollapseExample2">Toggle both elements</button>
21
</p>
22
<div class="row">
23
<div class="col">
24
<div class="collapse multi-collapse" id="multiCollapseExample1">
25
<div class="card card-body">
26
Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
27
</div>
28
</div>
29
</div>
30
<div class="col">
31
<div class="collapse multi-collapse" id="multiCollapseExample2">
32
<div class="card card-body">
33
Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
34
</div>
35
</div>
36
</div>
37
</div>
38
39
<!-- Optional JavaScript -->
40
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
41
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
42
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
43
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
44
</body>
45
46
</html>