Is there a way to switch between the content of different bootstrap buttons?

I'm currently using bootstrap and scss to develop a WordPress page, but I am encountering challenges in creating collapsible content. The goal is to have four buttons where only the content of one button is displayed at a time while the others remain collapsed. When a user clicks on a button, only that specific content should be visible while the rest are hidden.

Additionally, there is an active class intended to indicate which button has been clicked, but it is not functioning as expected. Any assistance or guidance on this matter would be greatly appreciated. Thank you!

    var mainStarter = {
      initialize: function() {
        this.buttonActive();
        this.toggleButton();
      },

      buttonActive: function() {
        $(".btn.btn-bg-md").on("click", function() {
          $(this).toggleClass('active');
        });
      },

      toggleButton: function() {
        $('.column button').click(function() {
          $(".collapse:visible").add($(this).next()).slideToggle();

        });
      },


      $(function() {
        mainStarter.initialize();
      });
      
.row {
  display: flex;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
  <!-- Item 1 -->
  <div class="column">
    <button type="button" class="btn btn-bg-md" data-toggle="collapse" data-target="#btn1" aria-pressed="false" autocomplete="off">
            <h6>Day 1</h6>
          </button>
  </div>
  <!-- Item 2 -->
  <div class="column">
    <button type="button" class="btn btn-bg-md" data-toggle="collapse" data-target="#btn2" aria-pressed="false" autocomplete="off">
            <h6>Day 2</h6>
          </button>
  </div>
  <!-- Item 3 -->
  <div class="column'>
    <button type="button" class="btn btn-bg-md" data-toggle="collapse" data-target="#btn3" aria-pressed="false" autocomplete="off">
            <h6>Day 3</h6>
          </button>
  </div>
  <!-- Item 4 -->
  <div class="column">
    <button type="button" class="btn btn-bg-md" data-toggle="collapse" data-target="#btn4" aria-pressed="false" autocomplete="off">
            <h6>Day 4</h6>
          </button>
  </div>

  <div class="container">
    <div class="collapse" id="btn1">
      <h1>Something 1</h1>
    </div>
  </div>
  <div class="container">
    <div class="collapse" id="btn2">
      <h1>Something 2</h1>
    </div>
  </div>
  <div class="container">
    <div class="collapse" id="btn3">
      <h1>Something 3</h1>
    </div>
  </div>
  <div class='container">
    <div class="collapse" id="btn4">
      <n1>Something 4</n1>
    </div>
  </div>

</div>

Answer №1

It seems like there may be some confusion about your request. From what I gathered, you are looking to have 2 buttons functionality - one to display the first message and hide the second message, and the other button to show the second message and hide the first. If this is correct, you can achieve this using jQuery by assigning the appropriate IDs to the div elements.

$("#button1").click(function() {
    if ($("#collapsebutton2").is(":visible")) {
    $("#collapsebutton2").toggle( "slow", function() {

    });
  }
  $("#collapsebutton1").toggle( "slow", function() {

  });

});

$("#button2").click(function() {
  $("#collapsebutton2").toggle( "slow", function() {

  });

  if ($("#collapsebutton2").is(":visible")) {
    $("#collapsebutton1").toggle( "slow", function() {

  });
  }
});

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

Update the central information while keeping the entire webpage intact

Hey there, I could really use some assistance! I'm working on a software documentation website that is similar to the mongodb documentation page. It has a header, sidebar (navbar menu), and main content area. Within the sidebar navbar menu, I have dr ...

Unexpected Vue error causing disruptions in production

While running my website locally with yarn serve, everything works flawlessly. However, when I attempt to run yarn build and then open the production version locally (using an Nginx server), I encounter an error when trying to access a specific globally- ...

Use jQuery to dynamically add a css class to a Bootstrap Card element only after the last checkbox inside has been checked

I've successfully implemented a jQuery script that adds one class and toggles another when a checkbox is checked. It functions as expected with single checkboxes, but I'm encountering issues when multiple checkboxes are placed inside. I want the ...

What disparities exist between utilizing Arial Black font and regular Arial font with the <Strong> tag?

Despite setting my text to Arial Black, it appears as regular text on Firefox. To workaround this issue, I tried using the Arial font with the Strong tag but couldn't notice any visual difference. Should I be concerned about this discrepancy? Thank y ...

Styling a custom Qt class using a CSS selector

I have developed a custom "Slider" widget as a subclass of QWidget and am looking to customize its appearance using Qt's stylesheets. Is there a way to define this widget to the Qt application so that any styling set in the application stylesheet will ...

Fetching real-time Twitter search feeds through dynamic AJAX requests

Previously, I successfully used JSON to retrieve hash tag feeds from Twitter and Facebook. However, currently the feeds are not updating dynamically, indicating a need for Ajax implementation. Unfortunately, my lack of knowledge in Ajax is hindering this ...

What is the best way to align a single row in the center and another row at the bottom using Bootstrap?

I'm struggling with aligning two rows within a section element that has a vh-100 height. The first row consists of 2 cols (headers, image, and dropdown) that I want to vertically align to the middle of the container. The second row just contains an i ...

Retrieval of targeted keywords within a given paragraph using JavaScript

Currently tackling a project in react native (JS based) where I'm tasked with identifying and extracting the names of hospitals from dynamic paragraphs sourced from the backend server. Traditional methods such as match(), includes(), and more aren&apo ...

Enhancing the ShaderMaterial attribute in three.js

Exploring the three.js tutorial on shaders, we discover a technique for updating uniform values of a ShaderMaterial: var attributes = { displacement: { type: 'f', // a float value: [] // an empty array } }; var uniforms = { amplitu ...

RStudio Connect now automatically inserts CSS code into cells when updating

My current project involves creating an app for holiday requests. The main page of the app is displayed like this: https://i.sstatic.net/z2qma.png At the bottom right corner, you'll notice a column labeled "Accepted?" which displays either "OK" or " ...

Error encountered: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, anticipating either a comma or a semicolon

I encountered this error after making changes to the source of my image. Prior to editing, everything was functioning correctly. Below is my complete code: <?php session_start(); require_once("/home/a9440778/public_html/registration/connect.php"); fu ...

What steps can I take to direct mobile visitors to the mobile-friendly version of my website?

Looking for a way to automatically redirect users on mobile devices from www.domain.com to the new mobile version at m.domain.com? ...

What is the best way to assign a CSS class to the jqGrid height property when using setGridHeight?

Our objective is to implement a media query css class. Currently, the code is utilizing $(window).height() to determine the height. $('#list').jqGrid('setGridHeight', $(window).height() - 380); Instead of relying on $(window).height( ...

Discovering the following element with jQuery

Looking to locate the next element in jQuery, the below code snippet is provided: <div> <input id="btn<%# Eval("Id") %>" type="button" value="fetch"/> </div> <div> <img id="imgLoading<%# Eval("Id") %>" style="width ...

Is it possible to transmit a WebRTC frame to a Python script?

I recently built my first web app using Python and Django, which displays webcam frames of clients. 'use strict'; // For this project, I am only streaming video (video: true). const mediaStreamConstraints = { video: true, }; // Video element ...

Iterating through JQuery using .each() to filter data in an XML file based on matching category types

Here is a function that displays each program collected from the previous function. The function works fine, but I want to refine it so that when a button is clicked, it only runs through each() for programs with a specific category like 'Jeu' ...

The border appears unnecessarily when clicking on the content area of the Understrap theme

Currently, I am in the process of building a WordPress theme using Understrap and its customstrap child theme from livecanvas. This project involves utilizing Bootstrap 4. I have successfully developed my theme, however, I am encountering an issue where a ...

Storing Django HTML input array data from _POST variables

Currently in the process of transitioning a website from PHP (Codeigniter) to Django, as I believe it will be a great learning experience. Most issues have been manageable so far, but I am encountering some difficulty with this particular one. In PHP, I c ...

Centering the logo using Material-UI's alignment feature

Need help centering a logo in my login form using Material-UI. Everything else is centered except for the logo, which is stuck to the left side of the card. I've tried adding align="center" and justify="center" under the img tag, but it's still ...

Retrieve the data file using input text with jQuery

Is there a way to extract data like this? data:application/pdf;base64,JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9UeXBlIC9DYXR…AKdHJhaWxlcgo8PC9TaXplIDE4Ci9Sb290IDEgMCBSPj4Kc3RhcnR4cmVmCjg4MzEzCiUlRU9G from an input file? $('#file').change(functio ...