Designing a feature to toggle between displaying and hiding different content sections

Can you make it so that when I click on the toggle checkbox, it will hide/show one of the two div elements?

The CSS provided below is for styling purposes only; and my HTML structure for the DIV elements cannot be altered.

input[type="checkbox"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer; }
  input[type="checkbox"]:focus {
    outline: 0; }

.toggle {
  height: 32px;
  width: 52px;
  border-radius: 16px;
  display: inline-block;
  position: relative;
  margin: 0;
  border: 2px solid #474755;
  background: linear-gradient(180deg, #2D2F39 0%, #1F2027 100%);
  transition: all .2s ease; }
  .toggle:after {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: white;
    box-shadow: 0 1px 2px rgba(44, 44, 44, 0.2);
    transition: all 0.2s cubic-bezier(0.5, 0.1, 0.75, 1.35); }
  .toggle:checked {
    border-color: #654FEC; }
    .toggle:checked:after {
      transform: translatex(20px); }
<div id="a">...</div>
<div id="b">...</div>
<div id="c">
  <div>
    <div>
      <input type="checkbox" class="toggle" checked>
    </div>
  </div>
</div>
<div id="d">...</div>
<div id="content-one">Content one</div>
<div id="content-two">Content two</div>
<div id="e">...</div>
</div>

EDIT:

I received a simplified version from ChatGPT (Bing Chat) that did not work:

  #content-two {
    display: none;
  }
  .toggle:checked ~ #content-two {
    display: normal;
  }
  .toggle:checked ~ #content-one {
    display: none;
  }
<div id="a">...</div>
<div id="b">...</div>
<div id="c">
  <div>
    <div>
      <input type="checkbox" class="toggle" checked>
    </div>
  </div>
</div>
<div id="d">...</div>
<div id="content-one">Content one</div>
<div id="content-two">Content two</div>
<div id="e">...</div>

Answer №1

To achieve the desired outcome, you can utilize :has() and :not(:has())

Keep in mind that :has() is currently only supported in the latest version of Firefox (version 121). Prior to this, it was accessible through a config switch but had some issues under certain circumstances.

It's worth noting that support for these selectors may be limited on mobile browsers, with better compatibility on main ones such as Chrome and Safari.

input[type="checkbox"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}

input[type="checkbox"]:focus {
  outline: 0;
}

.toggle {
  height: 32px;
  width: 52px;
  border-radius: 16px;
  display: inline-block;
  position: relative;
  margin: 0;
  border: 2px solid #474755;
  background: linear-gradient(180deg, #2D2F39 0%, #1F2027 100%);
  transition: all .2s ease;
}

.toggle:after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: white;
  box-shadow: 0 1px 2px rgba(44, 44, 44, 0.2);
  transition: all 0.2s cubic-bezier(0.5, 0.1, 0.75, 1.35);
}

.toggle:checked {
  border-color: #654FEC;
}

.toggle:checked:after {
  transform: translatex(20px);
}

#c:has(.toggle:checked) ~ [id=content-one] {
  display: none;
}
#c:not(:has(.toggle:checked)) ~ [id=content-two] {
  display: none;
}
<div id="a">...</div>
<div id="b">...</div>
<div id="c">
  <div>
    <div>
      <input type="checkbox" class="toggle" checked>
    </div>
  </div>
</div>
<div id="d">...</div>
<div id="content-one">Content one</div>
<div id="content-two">Content two</div>
<div id="e">...</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

Having trouble sourcing packages from npm or bower for download

After working with npm and bower using my mobile internet connection without any problems, I switched to my university proxy and made changes to the proxy configuration (npm config set proxy). Upon returning to my mobile internet and setting the proxy valu ...

Problem with Converting C# Objects into JavaScript

I need help transferring a double array from C# to JavaScript dynamically. Currently, I am using the following approach: var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); var jsLat = serializer.Serialize(lat); After that, I use ...

How can you target a custom tag using a wildcard in CSS?

Within my Angular application, I have various component pages such as <app-page-home>, <app-page-login>, <app-page-documentation>, etc. that are displayed when needed in the <router-outlet>. I am facing a challenge where I want to ...

Error encountered: Unexpected token found while trying to import react-native-fetch-blob

We are facing an issue while trying to incorporate the react-native-fetch-blob package into our project using the code snippet below: const RNFetchBlob = require('react-native-fetch-blob'); const firebase = require('firebase'); Howeve ...

Saving Information to a Specific Location on the Client Side in a CSV File

I am currently working on a static application that uses Angular JS technology. My goal is to write data into a CSV file at a specific path in order for another API to read the data from that location. Despite searching extensively on the internet, I hav ...

`html2canvas encountered an issue: Unable to locate a logger instance`

When I use html2canvas to render the same content repeatedly, I encounter an error about 5% of the time. It seems to be sporadic and I'm not sure why it occurs. What could be causing this unpredictable behavior? html2canvas.js:2396 Uncaught (in promi ...

What sets apart window.location.href from this.router.url?

I'm curious about the various methods of obtaining the current URL in Angular. For instance: this.router.url My main question is: What advantages does using this.router.url offer over simply using window.location? Could someone kindly provide an exp ...

What is the best way to create a scroll bar within a table while ensuring that the rows expand to fill the entire width?

I am attempting to design a table with 4 columns and a minimum of 12 rows while utilizing overflow-y:scroll. The webpage is completely responsive. I understand that in order to incorporate a scrollbar into the table, I must specify display: block for both ...

Finding if a certain id exists within a variable using JavaScript or jQuery

A PHP function is utilized to retrieve data from AJAX, resulting in the following: > Object {0: "<div class="message_text" id="297"><div > class="use…-$('.messages').prop('clientHeight'))</div></div>", 1: & ...

The SkyBox in THREE.js is a visually stunning feature that

I've been trying to create a SkyBox in THREE.js, following some tips I found online. However, none of the methods seem to work for me. I have double-checked the file paths and image paths, so I'm not sure what's causing the skybox not to app ...

Exploring the power of Jasmine with multiple spy functionalities

I'm currently working on writing unit tests for an Angular application using Jasmine, specifically focusing on testing different scenarios within a function. The main challenge I am facing is structuring the test to accommodate various conditions such ...

Angular application across multiple subdomains

Currently creating an angular application that allows users to generate and share digital books with a subdomain link like mycoolbook.theappsite.com. However, I've set up the routes so that editing books are at the URL "mycoolbook.theappsite.com/sett ...

Verifying the visibility status of a div and toggling a class based on it

I'm facing a challenge with multiple checkboxes in a row. When a button is clicked, it displays a div (#locale_container), and flips a small arrow by adding a rotated class. However, I only want the arrow to flip when the div is being shown or hidden ...

What is the most effective strategy for managing dependencies for npm packages?

I am currently working on extracting a few Vue.js components from the main application and converting them into an npm package stored in a repository. This package will be imported and utilized across two different websites. To bundle everything, I am util ...

Integrating AWS IVS quality plugin with video.js for enhanced video streaming in a Next JS application

Hello, I am new to AWS media services and recently started experimenting with AWS IVS for video streaming. I am currently working on integrating the AWS IVS quality plugin to Video.js. The player is up and running, displaying the video, but I encountered a ...

Submitting information to an HTML page for processing with a JavaScript function

I am currently working on an HTML page that includes a method operating at set intervals. window.setInterval(updateMake, 2000); function updateMake() { console.log(a); console.log(b); } The variables a and b are global variables on the HTML page. ...

Using jQuery Ajax to upload an image and pass it as model data to a Controller in MVC/Razor

By using Ajax/jquery, data in the view is sent to a controller without the use of a form. The Ajax call is triggered by a Click function. Razor View <div class="form-group"> @Html.LabelFor(model => model.AddAUser.UserAgeMonths, htmlAttributes: ...

Tips for incorporating an outside model into vue.js with babylon js

Struggling with importing a gltf file into vue.js using babylon.js to create a 3D view on a webpage. The documentation online isn't very clear, and I've tried the following steps in my Hello.vue file: <div> <h1> Hello </h1> < ...

Setting a default currency value (either in dollars or euros) in an input field

Is there a way to automatically insert a currency symbol (€, $) in a text field by default when entering a number? Are there any default values for this feature? Thank you Here is an example ...

The date and time displayed in the Kendo grid are incorrectly shown as strange values

I have a datetime column formatted like this: { field: "ModifiedDate", dataType: "System.Date", title: accountCodeResourceProvider.ModifiedDate, format: "{0:G}", width: 150, hidden: true }]; However, when an empty value cell appea ...