How to make Bootstrap 4 navbar tabs collapse using card accordion functionality

Check out this interactive example: https://jsfiddle.net/dominijk/bLpach25/1/

The accordion layout I have set up consists of collapsible cards where opening one card causes the others to collapse. The tabs within each card are functioning properly.

My goal is to create a link between the tabs and the card collapses. In other words, when a tab is clicked, its content should be displayed while other cards collapse. Similarly, if another card is opened, the tab content should collapse.

    <div id="accordion3">
    <div class="card">
    <div class="card-header">
      <a class="card-link" data-toggle="collapse" href="#collapseA">
             Colour map 
           </a>
      <!-- Add option to collapse all cards here as shown in https://codepen.io/dominijk/pen/JejNpp-->
    </div>
    <div id="collapseA" class="collapse show" data-parent="#accordion3">
      <div class="card-body" id=menu>
        <fieldset>
          <input id='radioOne' type='radio' name='rtoggle' value='radioOne'>
          <label for='radioOne'>One</label><br>
          <input id='radioTwo' type='radio' name='rtoggle' value='radioTwo'>
          <label for='radioTwo'>Two</label><br>
          <input id='radioThree' type='radio' name='rtoggle' value='radioThree' checked='checked'>
          <label for='radioThree'>Three</label><br>
        </fieldset>
      </div>
    </div>
  </div>
   <div id="accordion2">
   <div class="card">
   <div class="card-header collapse show">
   <a class="collapsed card-link" data-toggle="collapse" href="#collapseB">  
    Filter features by
    </a>
    <ul class="nav nav-tabs">
          <li class="collapsed card-link"><a class="nav-link" data-toggle="tab" 
    href="#tab-1" role="tab">Tab 1</a></li>
    <li class="nav-item"><a class="nav-link" data-toggle="tab" href="#tab- 
   2" role="tab">Tab 2</a></li>
          <li class="nav-item"><a class="nav-link" data-toggle="tab" href="#tab-3" role="tab">Tab 3</a></li>
        </ul>
        <div id="collapseB" class="card card-tabs-1" data-parent="#accordion2">
          <div class="card-block">
            <div class="tab-content">
              <div class="tab-pane active" id="tab-1" data-parent="#accordion2">
                <h4 class="card-title">Tab 1</h4>
                <p class="card-text">Content.</p>
              </div>
              <div class="tab-pane" id="tab-2">
                <h4 class="card-title">Tab 2</h4>
                <p class="card-text">Content.</p>
              </div>
              <div class="tab-pane" id="tab-3">
                <h4 class="card-title">Tab 3</h4>
                <p class="card-text">Content</p>
              </div>
            </div>
          </div>
        </div>

      </div>
    </div>
    <div class="card">
      <div class="card-header">
        <a class="collapsed card-link" data-toggle="collapse" href="#collapseC">
             Show boundaries for
           </a>
      </div>
      <div id="collapseC" class="collapse" data-parent="#accordion2">
        <div class="card-body">
          Card content can be <br>
          <span> span </span>
          <div> div </div>
        </div>
      </div>
    </div>
    <div class="card">
      <div class="card-header">
        <a class="collapsed card-link" data-toggle="collapse" href="#collapseD">
             Another panel
           </a>
      </div>
      <div id="collapseD" class="collapse" data-parent="#accordion2">
        <div class="card-body">
          Card content can be <br>
          <span> span </span>
          <div> div </div>
        </div>
      </div>
    </div>
  </div>
</div>

Answer №1

$(document).ready(function() {

  $('.nav-link').on('click', function() {
    if (!$('#collapseB').hasClass('show')) {
      $('#collapseB').collapse('toggle')
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
</script>
<div id="accordion3">
  <div class="card">
    <div class="card-header">
      <a class="card-link" data-toggle="collapse" href="#collapseA">Colour map</a>
    </div>
    <div id="collapseA" class="collapse show" data-parent="#accordion3">
      <div class="card-body" id=menu>
        <fieldset>
          <input id='radioOne' type='radio' name='rtoggle' value='radioOne'>
          <label for='radioOne'>One</label><br>
          <input id='radioTwo' type='radio' name='rtoggle' value='radioTwo'>
          <label for='radioTwo'>Two</label><br>
          <input id='radioThree' type='radio' name='rtoggle' value='radioThree' checked='checked'>
          <label for='radioThree'>Three</label><br>
        </fieldset>
      </div>
    </div>
  </div>
  <div class="card">
    ........
    .....
    </div>
  </div>
  <div class="card">
    ........
    .....
    </div>
  </div>
</div>

Fiddle

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the steps to incorporate an iframe containing uniquely designed XML content?

I'm currently working on a website project for a client who specifically wants to include news from the BBC. Despite spending 3 hours searching online, I haven't been able to successfully insert an XML file into an iframe and style it to the clie ...

Send the data from a div to a different page using PHP

Is there a way to pass the value displayed in DIV id to another page? The data is retrieved from another page and shown within the given code. <div id="res" style="float: left; Padding-left: 3px; padding-right:13px;line-height: 15px;"> ...... ...

What is the significance of receiving an error in Internet Explorer when running the code below?

function checkStepValidity(isValid, dataModel) { if (isValid) { updatedDataModel = mergeObjects(this.updatedDataModel, dataModel); } }, The code above encounters the following error in Internet Explorer / Edge browse ...

Resolving Div Alignment Issues in Internet Explorer with jQuery CSS AddClass and RemoveClass

I've set up an HTML table with a hover highlight feature using jQuery and CSS. Here's how my hover event is structured: $('#' + element.id + ' tr:not(:first,[class="summary_row"])').die('mouseover mouseout').live(&a ...

Errors are not displayed or validated when a FormControl is disabled in Angular 4

My FormControl is connected to an input element. <input matInput [formControl]="nameControl"> This setup looks like the following during initialization: this.nameControl = new FormControl({value: initValue, disabled: true}, [Validators.required, U ...

What is the way to instruct Mongoose to exclude a field from being saved?

Is there a way in Mongoose to instruct it to not save the age field if it's null or undefined? Or is there a method to achieve this in Express? Express router.put('/edit/:id', function(req, res) { Person.findOneAndUpdate({ _id: req.p ...

Is it possible to conceal the Google recaptcha badge using CSS and show the legal text in its place instead?

We have successfully hidden the Google reCaptcha badge on our website using CSS: <style> .grecaptcha-badge { visibility: hidden; } </style> As a result, there is now an empty space where the reCaptcha badge used to be display ...

Using Vue.js to Delete an Element from an Array

I am facing an issue with my form value where the IDs are not getting deleted when I remove data. Each time I save data, a new ID is added to the list in the form value. But when I delete any of those data entries, the corresponding ID is not removed from ...

Discord.JS Guild Member Cache Responses that are not recognized as valid

My automated messaging bot has been running smoothly for the past 6-8 months, but recently it encountered a strange issue with a specific user. Upon checking the cache of the Discord server it operates on, I noticed that it only returned two members - myse ...

Achieving vertical centering by adjusting padding within the parent container

I've successfully set up a DIV with a height of 200px and an inner div at 150px, leaving me 50px for the image caption. Now I want to vertically center the text within that remaining 50px. .captioned { width: 300px; height: 200px; display: fl ...

How can I implement SSO and Auth for multiple sub-domains using PHP?

Is it possible to implement SSO using PHP between two different sub-domains (e.g. parappawithfries.com and *.parappawithfries.com)? My current configuration is causing issues as I use Cloudflare to direct my subs to a different 000webhost website. While I ...

Stack the labels of separate datasets on top of each bar in a bar chart using Chartjs: How can this be achieved?

chart.js 4.4.2 chartjs-plugin-datalabels I am aiming to achieve this effect const chartCtr = document.querySelector('#temp-chart1') as HTMLCanvasElement; new Chart(chartCtr, { type: 'line', plugins: [ChartDataLabels], opt ...

What is the best way to incorporate regular, light, and bold fonts into your project using links from Google Fonts

I am looking for three specific styles of Ubuntu font without having to download them. To access these fonts, I inserted this link in the <link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,700" rel="stylesheet"> According to Google& ...

A step-by-step guide on duplicating the functionality of the jQuery

Issue at Hand: I'm attempting to recreate the functionality of jQuery's ajax method as I find myself utilizing XMLHttpRequest multiple times within a script. However, I am hesitant to include jQuery as a dependency since I only require a few meth ...

What is the best way to override @include within a CSS class?

Our team's application utilizes Bootstrap SASS for styling, but we have made certain customizations to the framework. The Bootstrap files are included in our project through a build process, making direct modifications impossible. When it comes to dr ...

Is there a way to automatically extend my content to fill the space on the page below the Material UI AppBar?

I am currently using React and Material UI React to develop my application. My goal is to implement the AppBar component with content underneath, without causing the entire page to scroll. I want the content to occupy the maximum remaining height and the f ...

My toggleclass function seems to be malfunctioning

I am encountering a strange issue with my jQuery script. It seems to work initially when I toggle between classes, but then requires an extra click every time I want to repeat the process. The classes switch as expected at first, but subsequent toggles req ...

Updating Variables in Azure DevOps Code Prior to DeploymentORModifying Variables

Can Azure DevOps automate code updates upon release? For instance, in my React app, I have a setting file with export const ISPROD = false. I want Azure DevOps to modify this value to true before building and deploying the app. Is this achievable? Please ...

Modify the state of each individual button

I recently came across a cool implementation using clicks and jQuery fade effects to show a div when a button is clicked and then change the state of the button. Check it out here: http://jsfiddle.net/ttj9J/5/ HTML <a class="link" href="#" data-rel="c ...

When a specific condition is met, Jquery can automatically execute a function contained

I am trying to slowly reveal the h1 tag using jQuery for demonstration purposes. The scroll function is working when I manually click to initiate it, but I am having trouble triggering it automatically. I feel like I might be missing something simple and ...