"Conceal the DIV only when neither of the switches are turned on

Is there a way to hide a paragraph only when two collapsible switch buttons are turned off in my Bootstrap frontend form? I have two checkboxes with corresponding switches, and the paragraph should only be hidden when both switches are off, but I'm not sure how to achieve this.

<label class="switch mt-2">
<input class="form-check-input" type="checkbox" role="switch" id="ds-activate" data-bs-toggle='collapse' data-bs-target='#dsForm' aria-expanded="false" aria-controls="dsForm" checked>
<div class="slider round">
</div>
</label>

<label class="switch mt-2">
<input class="fo rm-check-input" type="checkbox" role="switch" id="email-activate" data-bs-toggle='collapse' data-bs-target='#emailForm' aria-expanded="false">
<div class="slider round">
</div>
</label>

<div id="hideWhenBothSwitchOff" aria-expanded="false" class="collapse show">
  <label class="form-label">MORE OPTIONSs</label>
  <p>Explanation of the next options.</p>
</div>

<div id="dsForm" aria-expanded="false" class="collapse show">
  <div class="form-check mt-2 mb-2">
    <input class="form-check-input" type="checkbox" value="" id="all-documents">
    <label class="form-check-label" for="allDocuments">
      Checkbox 1 label
    </label>
  </div>
</div>

<div id="emailForm" aria-expanded="false" class="collapse">
  <div class="form-check">
    <input class="form-check-input" type="checkbox" value="" id="all-emails">
    <label class="form-check-label" for="allEmails">
      Checkbox 2 label
    </label>
  </div>
</div>

Answer №1

To achieve your goal, we can utilize a bit of JavaScript with the help of Bootstrap's Collapse component.

// Let's create a collapsible feature for the element that needs to be shown or hidden based on two checkboxes.
const collapsible = new bootstrap.Collapse('#hideWhenBothSwitchOff', {
  toggle: false,
});

// These are the checkboxes we need to monitor.
const checkboxes = [
  document.getElementById('ds-activate'),
  document.getElementById('email-activate'),
];

// We'll add event listeners to each checkbox to track their changes (checked or unchecked).
checkboxes.forEach((checkbox) => {
  checkbox.addEventListener('change', () => {
    // If any of the checkboxes is checked, display the collapsible element, otherwise hide it.
    const operation = checkboxes.some(({ checked }) => checked)
      ? 'show'
      : 'hide';
    collapsible[operation]();
  });
});
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f4d40405b5c5b5d4e5f6f1a011d011f">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06646969727572746776463328342836">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
<label class="switch mt-2">
<input class="form-check-input" type="checkbox" role="switch" id="ds-activate" data-bs-toggle='collapse' data-bs-target='#dsForm' aria-expanded="false" aria-controls="dsForm" checked>
<div class="slider round">
</div>
</label>

<label class="switch mt-2">
<input class="fo rm-check-input" type="checkbox" role="switch" id="email-activate" data-bs-toggle='collapse' data-bs-target='#emailForm' aria-expanded="false">
<div class="slider round">
</div>
</label>

<div id="hideWhenBothSwitchOff" aria-expanded="false" class="collapse show">
  <label class="form-label">MORE OPTIONSs</label>
  <p>Explanation of the next options.</p>
</div>

<div id="dsForm" aria-expanded="false" class="collapse show">
  <div class="form-check mt-2 mb-2">
    <input class="form-check-input" type="checkbox" value="" id="all-documents">
    <label class="form-check-label" for="allDocuments">
      Checkbox 1 label
    </label>
  </div>
</div>

<div id="emailForm" aria-expanded="false" class="collapse">
  <div class="form-check">
    <input class="form-check-input" type="checkbox" value="" id="all-emails">
    <label class="form-check-label" for="allEmails">
      Checkbox 2 label
    </label>
  </div>
</div>

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

Build a photo carousel similar to a YouTube channel

Is there a way to create an image slider similar to the one on YouTube channels? I've noticed that when there are multiple videos on a channel, YouTube displays them in a slider with back and forth buttons to navigate through the videos that aren&apos ...

What is the best way to retrieve the identity or specifics of the item that was right-clicked within the context menu

My AngularJS populated table includes a link button. When I right-click on this button, I've created a specific context menu using jQuery that pops up. The issue arises when I try to retrieve the ID of the item I clicked on in the context menu (such a ...

React js background image not filling the entire screen

Having experience with React Native, I decided to give ReactJS a try. However, I'm struggling with styling my components because CSS is not my strong suit. To build a small web application, I am using the Ant Design UI framework. Currently, I have a ...

Trouble with follow-up ajax request in jQuery in MVC 4 app

I have a partial view containing a form, and when I click a button I make an ajax call. The controller returns the partial view and it renders correctly. Everything works perfectly the first time, but subsequent clicks on the button do not trigger another ...

Is it possible to animate CSS Grid components?

Is it possible to animate a re-ordered set of elements in an array that are arranged using a CSS Grid layout? Check out this quick boilerplate ...

Looking for a different option than JSON.stringify()? Check out JSON2.js for

Currently, I am attempting to initiate a WCF call to a function that requires one String parameter. To pass these parameters from jQuery, I utilize JSON.stringify(parameters), which consists of a name:value collection with the specified parameters. My con ...

SignalR enables the display of identical dashboard data pulled from queries on various browsers. By utilizing SignalR/hub, MVC, and C#.NET, the data can

Encountering an issue with my signalr/hub while fetching dashboard data. I'm using 2 browsers to access data based on dates. However, when searching for July on browser 1 and then switching to another month on browser 2, the data in browser 1 gets upd ...

What could be the reason my HTML5 application is not working offline on my android device?

Hello, I am encountering a problem and I could really use your assistance in figuring out what I am missing. There is a webpage that showcases numerous HTML5 mobile/web applications which can be found at For instance, let's look at this app where yo ...

How to create a slideToggle effect for nested ul and li elements

Initially, everything was working fine until I put it through the W3C validator. After fixing the errors, now it's not functioning as expected. I have created a jsfiddle The setup involves nested ul elements being used as dropdowns, with headers tha ...

How can I design a search box similar to the one on the android.com website?

Is it possible to replicate the search box effect on the Android website, where the menu items fade out and the search box expands upon clicking the search icon?view image here See the difference before and after clicking the search icon ...

Changing styles using jQuery when hovering over an element

Here is an example of HTML code: <li> <p> <a href="#" class="link" ><img src="images/photo_cityguild_cropped.jpg" alt="Education" title="Education"> <span class="label">E D U C A T I O N</span> </ ...

Tips on rearranging the location of a div element within a directive

I have created a custom directive that displays two divs, Div1 and Div2, with a splitter in the middle: Splitter Image Now, I am looking for a way to swap the positions of these two divs dynamically using an Angular directive. I thought about using ng-swi ...

React JS tutorial: deleting an entry from the browser's localStorage

I am currently working on an App that fetches data from an API and updates it randomly every 3 seconds. The user has the ability to print the data by clicking a button, which can also be used to stop the random updates. I have managed to implement this fun ...

Utilizing JQuery to track DIV clicks

I am encountering an issue with DIV clicks while using JQuery. I need guidance on this problem. Within my HTML, there are three designated DIV elements: <html> <body> <div id="overviewContainer"> <div id="firstContainer"></ ...

Revamp a dynamically generated class using ajax

I am currently developing an email-based application using PHP and the CodeIgniter framework. My goal is to change the status of unread messages to read when they are clicked on, utilizing two different classes: read0 and read1. While I can view the messag ...

Optimizing JavaScript for efficient handling of large arrays

I'm currently developing an image editing program using JavaScript to expand my knowledge of the language. The images I am working with are typically around 3000 x 4000 pixels in size, resulting in a total of 48,000,000 values to manage when convertin ...

How can one use an ajax call to delete rows from a table and then add new rows in their

Hey everyone, I'm looking to make an AJAX call and when it's successful, I want to remove all existing elements in my table and replace them with new ones based on the returned data. This is the method I have for making the AJAX call: $(document ...

Height specification in Tailwind Grid

Hi there! I'm currently working on implementing a grid system in NextJS using Tailwind CSS. However, I've hit a roadblock when it comes to automatically sizing the grids to fit the parent element. Let me illustrate my issue with two images below ...

Enhancing react-to-print functionality for optimal performance across various browsers and mobile platforms

Currently, I am developing a sample react-to-print resume template to be included in a larger portfolio website. While testing the page on Chrome and Edge browsers on a laptop, everything seems optimized. However, there are issues when using Firefox; the b ...

What could be the reason for the sudden failure of my jQuery + AJAX functionality?

As a novice in JavaScript/jQuery/AJAX, I have a suspicion that the issue lies in some typo that I may have overlooked. Everything was working perfectly, but when I made some edits, the hide() + show() methods stopped functioning (I tested it on both Firefo ...