Is there a method to activate the "Maintab 2 Content" and display the accordion content for "Collapsible Group Item #2" when clicking on the "Button for Maintab 2"? I am trying to implement similar functionality on other tabs as well, but I'm struggling with using events like "show.bs.tab", "show.bs.collapse", or other related events.
Feel free to review the code snippet below. You can also view the codepen demo here: https://codepen.io/graydirt/pen/eYawXvg
$(document).ready(function(){
$('#btn-main2').on('click', function(){
$('#main2-tab').tab('show');
$('#collapseTwo').collapse('show');
// Scroll to the accordion header
setTimeout(function() {
$('html, body').animate({
scrollTop: $('#collapseTwo').prev('.card-header').offset().top
}, 500);
}, 500);
});
});
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7c5c8c8d3d4d3d5c6d7e79389978997">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6c61617a7d7a7c6f7e4e3a203e203e">[email protected]</a>/dist/js/bootstrap.min.js"></script>
<ul style="list-style: none" class="py-5 d-flex align-items-center justify-content-center">
<li class="px-2">
<a id="btn-main2" class="btn btn-primary text-white" role="button">Button for Maintab 2</a>
</li>
</ul>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="main1-tab" data-toggle="tab" href="#main1" role="tab" aria-controls="home" aria-selected="true">Maintab 1 Content</a>
</li>
<li class="nav-item">
<a class="nav-link" id="main2-tab" data-toggle="tab" href="#main2" role="tab" aria-controls="profile" aria-selected="false">Maintab 2 Content</a>
</li>
<li class="nav-item">
<a class="nav-link" id="main3-tab" data-toggle="tab" href="#main3" role="tab" aria-controls="contact" aria-selected="false">Maintab 3 Content</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="main1" role="tabpanel" aria-labelledby="home-tab">main1</div>
<div class="tab-pane fade" id="main2" role="tabpanel" aria-labelledby="profile-tab">
<div id="accordion">
<div class="card">
<div class="card-header">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true">
Collapsible Group Item #1
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" data-parent="#accordion">
<div class="card-body">
<!-- Accordion content here -->
</div>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="mb-0">
<button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false">
Collapsible Group Item #2
</button>
</h5>
</div>
<div id="collapseTwo" class="collapse" data-parent="#accordion">
<div class="card-body">
<!-- Accordion content for item #2 goes here -->
</div>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="mb-0">
<button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false">
Collapsible Group Item #3
</button>
</h5>
</div>
<div id="collapseThree" class="collapse" data-parent="#accordion">
<div class="card-body">
<!-- Accordion content for item #3 goes here -->
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane fade" id="main3" role="tabpanel" aria-labelledby="contact-tab">main3</div>
</div>