Simple method for displaying or hiding divs depending on the status of a selected radio button using CSS only

I am currently working on showing or hiding div elements based on the status of a radio button. These divs are located after the input buttons in a different section of the page, and I am facing challenges in referencing them accurately.

This code is for an embedded donation form, hence, I cannot modify the HTML directly. My only option is to make changes to the CSS...

For the relevant code snippet, you can refer to this JSFiddle link: JS FIDDLE OF MY PROBLEM

I have gone through various posts discussing similar tasks with sibling or child elements. However, none of those solutions seem to align with my situation involving less direct connections between elements. Additionally, I acknowledge that my "~" connector may not be entirely accurate, but it serves as a placeholder. Thank you for any assistance provided.

Here's the pertinent HTML:

div#IATS_PaymentItemDiv_Comment,
div#IATS_PaymentItemDiv_Item2 {
  display: none;
}
input#IATS_Amount_Order_2:checked ~ div#IATS_PaymentItemDiv_Comment {
  display: block;
}
input#IATS_Amount_Order_2:checked ~ div#IATS_PaymentItemDiv_Item2 {
  display: block;
}
<div class="IATS_Div" id="IATS_PaymentBoxDiv">
  <form name="IATSPaymentBoxForm" id="IATSPaymentBoxForm" action="javascript:">
    <div class="IATS_InnerDiv">
      <div class="IATS_HeaderDiv"></div>
      <div class="IATS_ContentDiv" id="IATS_ContentSectionDiv">
        <label class="IATS_EventTitle">Join the Sierra Avalanche Center <span class="form-required">*</span>
        </label>
        <div class="IATS_AmountDiv" id="IATS_AmountSectionDiv">
          <div class="IATS_AmountItemDiv">
            <input type="radio" name="IATS_NewAmountListItem" class="IATS_RadioAmtButton" id="IATS_Amount_Order_0" value="35" checked="checked" />
            <label class="IATS_RadioAmtLabel" for="Amount_Order_0" id="IATS_AmountName_Order_0">$35</label>
            <div class="IATS_QtyDiv" style="display: none;">
              <button class="IATS_IncreaseQty" type="button">+</button>
              <input type="input" id="IATS_Payment_Qty0" class="IATS_QtyInput" maxlength="6" />
              <button type="button" class="IATS_IncreaseQty">-</button>
            </div>
          </div>
          <div class="IATS_AmountItemDiv">
            <input type="radio" name="IATS_NewAmountListItem" class="IATS_RadioAmtButton" id="IATS_Amount_Order_1" value="50" />
            <label class="IATS_RadioAmtLabel" for="Amount_Order_1" id="IATS_AmountName_Order_1">$50</label>
            <div class="IATS_QtyDiv" style="display: none;">
              <button class="IATS_IncreaseQty" type="button">+</button>
              <input type="input" id="IATS_Payment_Qty1" class="IATS_QtyInput" maxlength="6" />
              <button type="button" class="IATS_IncreaseQty">-</button>
            </div>
          </div>
          <div class="IATS_AmountItemDiv">


            <!--HERE IS THE RADIO BUTTON I WANT TO 'LISTEN' FOR -->


            <input type="radio" name="IATS_NewAmountListItem" class="IATS_RadioAmtButton" id="IATS_Amount_Order_2" value="100" />
            <label class="IATS_RadioAmtLabel" for="Amount_Order_2" id="IATS_AmountName_Order_2">$100</label>
            <div class="IATS_QtyDiv" style="display: none;">
              <button class="IATS_IncreaseQty" type="button">+</button>
              <input type="input" id="IATS_Payment_Qty2" class="IATS_QtyInput" maxlength="6" />
              <button type="button" class="IATS_IncreaseQty">-</button>
            </div>
          </div>


         […]

          <div class="IATS_TotalDiv" style="display: none;">Total: $<span id="IATS_Payment_TotalAmount" value="0.00">100.00</span>
            <input name="IATS_AmountListItem" id="IATS_AmountListItem" style="display: none;" />
            <input name="IATS_AmountListItem_OtherAmount" id="IATS_AmountListItem_OtherAmount" style="display: none;" />
          </div><span class="IATS_PaymentLabelOnLeft IATS_PaymentMinimumErrorMessage" id="IATS_PaymentItemErrorMesssage_NonAmount">Please select at least one
            amount item.</span>

        </div>
        <div class="IATS_PaymentDiv" id="IATS_PaymentSectionDiv"> <span class="IATS_PaymentSectionTitle">Payment Information</span><span class="IATS_PaymentSectionNote"><b>Bold</b> field is required input</span>


           […]

        </div>
  </form>
  </div>

Answer №1

To style your elements using CSS selectors, take a look at this example.

http://jsfiddle.net/fosz775j/1/

<input type="checkbox" id="test" name="test"> 
<label for="test">your text</label>

input[type=checkbox] + label {
  color: #ccc;
  font-style: italic;
} 
input[type=checkbox]:checked + label {
  color: #f00;
    display: none; /* HIDE YOUR ELEMENT */
  font-style: normal;
}

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 is the best way to display only a specific container from a page within an IFRAME?

Taking the example into consideration: Imagine a scenario where you have a webpage containing numerous DIVs. Now, the goal is to render a single DIV and its child DIVs within an IFrame. Upon rendering the following code, you'll notice a black box ag ...

"Customize Your CSS Dropdown Menu with Dynamic Background Color for div Elements

I recently put together a website dedicated to watches. Feel free to check it out at horology dot info. (Please refrain from sharing the direct URL of my site in your reply.) I'm curious as to why the dropdown menu, sourced from , is showing up unde ...

Display a single image on the tablet and a distinct image on the computer

Currently, I am encountering an issue with my webpage located at . The problem lies in the right upper corner where a ribbon is located. However, when viewing the page on a tablet, the ribbon overlaps with my menu. To resolve this, I thought about displa ...

Tips for ensuring consistent alignment of the final navigation item

I'm having trouble understanding why the layout collapses when I resize the window horizontally. I've attempted to adjust the width of the div and tried using display:block. My website is built with Bootstrap 3, although I don't believe that ...

Items in the Vue.Draggable display can be arranged on both the left and right

Recently delving into Vue.js, I am utilizing Vuedraggable for item dragging in my project. Currently, the items in the draggable div are shown as Text 1 Text 2 Text 3 Text 4 Is there a way to rearrange them to display like this? Text 1 Text 2 Text 3 T ...

The button is only partially visible

My HTML code for a small web app has a button that says "ok," but it's too tiny to display the text fully. I wonder if I'm missing something with the span element? How can I enlarge the button so that it's bigger and shows the entire "ok" ...

Tips for alternating the color of <li> or <tr> elements consecutively

Looking to alternate the background color of li or tr elements consecutively. Please note: A static class will not work as I need to call this in a while loop. The desired output should look like: <li>line1</li> //white background <li> ...

When utilizing the "as" attribute, styled components do not inherit any styles

I am currently utilizing both styled-system and styled components, working on a simple scenario like the one below: const buttonFont = { fontFamily: "Chilanka" }; // define basic text styling const Text = styled.div` ${typography} ${color} `; // c ...

Include text for both the button label before and after a bootstrap toggle button

Is there a way to change the button label on a bootstrap toggle switch/button before and after clicking it without using jquery or javascript? It seems like there should be a feature in bootstrap for this. Initially, the label reads: Show list After clic ...

Issues with cascading style sheet navigation designs

Working on this project is new to me and I am encountering a challenge with two issues. My goal is to create a menu display similar to the image shown here: https://i.sstatic.net/FsHq5.png The problem lies in my inability to underline the selected optio ...

Placing divs in rows and columns at specific breakpoints

I have a footer with two divs that I need to be centered and side by side. When the screen size reaches 960px, I want the second div to move below the first one while still staying centered. Does anyone know how to achieve this? Below is my code snippet: ...

Limiting the size of images within a specific section using CSS

When using CSS, I know how to resize images with the code snippets below: img {width:100%; height: auto; } img {max-width: 600px} While this method works effectively, it applies to every image on the page. What I really need is for some images to be ...

Creating a simulated dropdown menu using only CSS

I was able to create a select menu using only CSS, following some code I found. However, I am now facing an issue with overlaying divs. Whenever I hover or click on the "select menu", all the divs below it move down. I've attempted to adjust the z-i ...

Is there a way to customize the slicing of *ngFor in a component according to the components it is being injected into?

This code snippet represents a component that needs to be included in other components: <div class="row"> <div class="col-12 [...]" *ngFor="let course of courses"> <div class="card"> ...

Choosing HTML elements within nested DIV tags

In the process of creating a Hangman-style game using javascript/jQuery, I encountered an issue with selecting HTML content from virtual keyboard keys : <div class="square keyboard"> <div class="content"> <div class="table"> ...

problems with the way white space is displayed on the right side of the page in Opera and Safari

Hello, I am having an issue where Opera and Safari are displaying extra white space on the right when I use percentages for my width. Below is my code snippet: Safari 5.1 and Opera 11.11 View sample fiddle here HTML: <!DOCTYPE html PUBLIC "-//W3C//D ...

Find all the child elements within a specific class using CSS

Looking for a method to effortlessly apply styles to all elements within a specific class without repeating code like this: .element-wrapper .coin-box{ margin: 10px; float: left; } .element-wrapper .platform{ margin: 10px; float: left; } .eleme ...

What is the method for altering the "return" of a CSS transition?

Today, I delved into the world of transitions and I must say, they are amazing and have great potential for future websites. Take a look at my current code: http://jsfiddle.net/Ldyyyf6n/ I am looking to tweak the "return" transition of the circle/square ...

In JavaFX, when adjusting the transparency of a popup window, only the brightness changes while the solid color remains unchanged, effectively concealing

Is it possible to have a transparent popup window (errorDialog.fxml) on top of the main window (mainWindow.fxml) with 50% opacity while still showing the content of the main window underneath? My attempts at setting the background color of the overlay insi ...

Align title text to the right within the Material Design Card

I have implemented a Material Design (MDL) card to showcase numeric values in the title. However, I am facing an issue where the values are left aligned by default and I want them to be right aligned. Despite trying various styles, they are being ignored ...