Guide on implementing a text display switch using CSS

I am working on two radio type menus to allow users to select different payment methods which will reveal corresponding information. However, I am facing a challenge in implementing the Precautions content below. Can someone guide me on how to achieve this functionality?

Here is an example of what I am trying to do: When a user clicks on cash payment, I want the Precautions section to display notes related to cash payments. Similarly, when they click on credit card payment, the Precautions section should show notes for credit card payments.

If anyone has experience with this, please share your insights. Is it possible to accomplish this without relying solely on JavaScript? And if JS is required, how would you suggest writing the code?

input {
  display: none;
}

.radio {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.radio .radio-list {
  display: flex;
  align-items: center;
}

.radio .radio-list .radio-sign {
  position: relative;
  display: inline-block;
  text-indent: -9999px;
  width: 20px;
  min-width: 20px;
  height: 20px;
  border-radius: 50vh;
  border: 2px solid #7f7f7f;
  background: #fff;
  margin-right: 8px;
}

.radio .radio-list .radio-sign:before {
  content: "";
  width: 10px;
  height: 10px;
  background: #000;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50vh;
  display: none;
}

.from-box {
  display: none;
  padding: 20px;
  background-color: #ccc;
  border: 1px solid #222;
  border-radius: 6px;
}

input:checked~.from-box {
  display: block;
}

input:checked~.radio-list .radio-sign::before {
  display: inline-block !important;
}

.wrap {
  margin: 10px auto;
  padding: 30px;
  background-color: #ccc;
  text-align: center;
}

.wrap h2 {
  font-size: 20px;
  font-weight: 900;
  margin-bottom: 20px;
}

.wrap .cash_tips {
  display: block;
  color: red;
}

.wrap .credit_tips {
  display: none;
  color: blue;
}
<label class="radio">
  <input type="radio" value="" name="text">
  <div class="radio-list">
    <figure class="radio-sign">radio-sign</figure>
    <p>Pay by cash</p>
  </div>
  <div class="from-box">
    Pay by cash info...
  </div>
</label>

<label class="radio">
  <input type="radio" value="" name="text">
  <div class="radio-list">
    <figure class="radio-sign">radio-sign</figure>
    <p>credit payment</p>
  </div>
  <div class="from-box">
    credit payment info...
  </div>
</label>


<div class="wrap">
  <h2>Precautions</h2>
  <ul class="cash_tips">
    <li>1.Cash Payment Notes Clause</li>
    <li>2.Cash Payment Notes Clause</li>
  </ul>

  <ul class="credit_tips">
    <li>1.Cash Payment Notes Clause</li>
    <li>2.Cash Payment Notes Clause</li>
  </ul>
</div>

Answer №1

By using JavaScript, you have the ability to generate markup that can be inserted into the parent UL class (referred to as '.tips' in this case). Whenever the cash or credit option is clicked, the inner HTML of the UL is dynamically updated.

const cashContainer = document.querySelector(".cash-container");
const creditContainer = document.querySelector(".credit-container");
const tipsList = document.querySelector(".tips");
const creditItems = `
  <li>Details about credit items will go here</li>
  <li>Additional information for credit items can go here too</li>
  <li>Feel free to add more content here</li>
`;
const cashItems = `
  <li>Information about cash items goes here</li>
  <li>More details for cash items can be added here</li>
  <li>And more content can be included here</li>
`;

const handleCreditClick = () => {
  tipsList.innerHTML = creditItems;
}

const handleCashClick = () => {
  tipsList.innerHTML = cashItems;
}

cashContainer.addEventListener("click", handleCashClick);
creditContainer.addEventListener("click", handleCreditClick);

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

Unable to retrieve cookie using getServerSideProps in Next JS

My goal is to retrieve and return two cookies, but I am running into an issue where only the token cookie is returned successfully, while the key cookie remains inaccessible. export async function getServerSideProps(ctx) { const cookies = ctx.req.header ...

Django RGBField cannot locate jQuery

Working on a project that utilizes an RGBField, the following script is injected into the template (nested deep within django's structure, as its exact location remains elusive): <script type="text/javascript"> (function($){ ...

Angular's $q.defer() function will yield an object with a "then" function

In my Angular file, I am attempting to access a database using $http and then store the retrieved data in a $scope variable for display on the webpage. However, I am encountering difficulties with $q.defer not running as expected. When I check the consol ...

Disable automatic reloading once the AJAX call is successful

After scanning a barcode on my page, it automatically separates into two autocomplete fields. Subsequently, Ajax sends this data to the database in order to retrieve some results, which are then populated in the autocomplete fields. The issue arises when ...

Icon displayed within the input field

Is there a simple method to add an input text element with a clear icon on the right side, similar to the layout of the Google search box? I've searched, but I could only find instructions for placing an icon as the background of the input element. I ...

The compiler detected an unanticipated keyword or identifier at line 1434. The function implementation is either absent or not placed directly after the declaration at line 239

Having trouble implementing keyboard functionality into my snake game project using typescript. The code is throwing errors on lines 42 and 43, specifically: When hovering over the "window" keyword, the error reads: Parsing error: ';' expecte ...

What could be the reason for receiving an undefined user ID when trying to pass it through my URL?

Currently, I am in the process of constructing a profile page and aiming to display authenticated user data on it. The API call functions correctly with the user's ID, and manually entering the ID into the URL on the front end also works. However, wh ...

Tips for Utilizing CSS Overflow: Hidden to Prevent the Parent Container from Expanding

I have a fixed width column and a fluid column. In the fluid column, I want to display a string with nowrap so that it does not overflow the container. Here is an illustration of how I want the text to appear: --------------------------------------------- ...

Dealing with Mouse-Operated Data Deletion in a TextBox

I have a unique requirement that involves displaying a button labeled Set only when the text in two separate Text input fields matches. If the text in both fields matches, I am successfully able to control the visibility of the Set Button. However, I enc ...

The tab panels are failing to function properly due to the maxcdn CSS integration

I am struggling to make my basic JQuery code work, specifically to show and hide panels when a list item in the menu is clicked. The CSS I am using is from max cdn, so I'm unsure if I need to create my own CSS file for this. <!DOCTYPE html> ...

Tips for verifying if an object has been loaded prior to binding it with jQuery

When loading a "stub" file dynamically with jQuery that contains HTML objects, I aim to bind events to the loaded objects after they have finished loading. An example of what my external file may contain: <div id='myDiv'>click me</div& ...

Customize chrome's default shortcuts with JavaScript

I'm working on an application that requires me to override some shortcut keys in the Chrome browser. While I'm able to create custom shortcuts to trigger alerts like in this Stackblitz example, I'm having trouble overriding a few default sho ...

Compare the versions of React used in the linked library and the workspace application

As I work on developing a React library containing my commonly used components, I have organized my project structure in the following way: In the root folder, there is my rollup config file and the src/ folder which houses my library. Upon building, the ...

Processing data from a Buffer object using the .map() method and sending it as props to a component

As I work on my application, I encounter a challenge when dealing with a Buffer object that contains data from a file. My intention is to render the component Bar for each byte in this buffer and pass the byte as a prop. However, I am facing an issue with ...

The two Bootstrap columns are not displaying properly on the screen

I'm working on a website that features a gallery. The code below should ideally display two columns of photos when viewed on a medium device width (768px up to 992px). However, only one column is showing up on the screen. I'm hesitant to group th ...

Error encountered: Could not execute 'getComputedStyle' on the 'Window' object - the first parameter is not considered an 'Element' within the JavaScript map function

I've been searching for a solution to my problem both on StackOverflow and Google, but haven't been able to find one yet. So, here's my issue. I have an array of DOM elements that I retrieve using the @ViewChildren('labelSquare') ...

When clicking on Submit, the code does not acknowledge the Verify Function

I am currently facing an issue with implementing the JQuery Ui function "Verify();" to ensure that the name, age, country, and date fields are completed before allowing form submission. Even though I have written the verify function to execute when the us ...

How can you ensure a code snippet in JavaScript runs only a single time?

I have a scenario where I need to dynamically save my .env content from the AWS secrets manager, but I only want to do this once when the server starts. What would be the best approach for this situation? My project is utilizing TypeScript: getSecrets(&qu ...

simultaneous image hover effect for all images

I am experiencing an issue with my CSS and Bootstrap hover effect. The overlay tags are enclosed within a div class, but they all hover simultaneously. Each overlay belongs to a different Bootstrap grid, yet the effect extends even to the empty spaces betw ...

What are the similarities between using the map function in AngularJS and the $map function in jQuery?

I am currently in the process of migrating jQuery code to AngularJS. I have encountered some instances where the map function is used in jQuery, and I need to replicate the same functionality in AngularJS. Below is the code snippet that demonstrates this. ...