Display the div when the radio button input is clicked

I'm trying to display multiple divs if a radio button is checked in my code. Here's what I have attempted:

HTML:

<div class="form-group row">
                     <label for="inputEmail3" class="col-sm-2 col-form-label">Do you have Extras Cover</label>
                     <div class="col-sm-10">
                      <div class="form-check form-check-inline">
                        <input class="form-check-input yes" name="radio" type="radio"  id="yes" value="yes">
                        <label class="form-check-label" for="yes"> Yes </label>
                      </div>
                      <div class="form-check form-check-inline">
                        <input class="form-check-input" name="radio"  type="radio"  id="no" value="no">
                        <label class="form-check-label" for="no"> No </label>
                      </div>
                     </div>
                   </div>
                    <div class="form-group row show">
                     <label for="" class="col-sm-2 col-form-label">Medicare Number</label>
                     <div class="col-sm-10">
                       <input type="text" class="form-control" >
                     </div>
                   </div>
                   <div class="form-group row show">
                     <label for="" class="col-sm-2 col-form-label">Medicare Address</label>
                     <div class="col-sm-10">
                       <input type="text" class="form-control"  >
                     </div>
                   </div>

CSS:

.show{
display:none ;
}
input#yes:checked ~ div.show{
display: block;
}

When the 'YES' button is checked, the 'SHOW' class div will be displayed. It works when I place the 'SHOW' class div with the input field, but not outside of it.

If someone could assist me with correcting my code structure, I would greatly appreciate it.

For a visual representation, please refer to the image here: my code

Answer №1

If you want the 'show' divs to be outside the parent of the radio buttons, a css solution may not work and you might need to incorporate some javascript code instead.

document.querySelector('input#yes').addEventListener('click', function() {
  Array.from(document.querySelectorAll('.form-group.show')).forEach((group) => {
    group.style.display = "block";
  })
})

document.querySelector('input#no').addEventListener('click', function() {
  Array.from(document.querySelectorAll('.form-group.show')).forEach((group) => {
    group.style.display = "none";
  })
})
.form-group.show {
  display: none;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="form-group row">
  <label for="inputEmail3" class="col-sm-2 col-form-label">Do you have Extras Cover</label>
  <div class="col-sm-10">
    <div class="form-check form-check-inline">
      <input class="form-check-input yes" name="radio" type="radio" id="yes" value="yes">
      <label class="form-check-label" for="yes"> Yes </label>
    </div>
    <div class="form-check form-check-inline">
      <input class="form-check-input" name="radio" type="radio" id="no" value="no">
      <label class="form-check-label" for="no"> No </label>
    </div>
  </div>
</div>
<div class="form-group row show">
  <label for="" class="col-sm-2 col-form-label">Medicare Number</label>
  <div class="col-sm-10">
    <input type="text" class="form-control">
  </div>
</div>
<div class="form-group row show">
  <label for="" class="col-sm-2 col-form-label">Medicare Address</label>
  <div class="col-sm-10">
    <input type="text" class="form-control">
  </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

Showing a vertical arrangement of lists containing updated information

I am struggling to create lists that seamlessly flow together to showcase data. I am using the following website as a reference: I have examined the style sheet thoroughly but I cannot figure out how they are achieving this effect. I am hoping someone can ...

The webpage is completely blank, displaying only a stark white screen

After designing a website, I encountered an issue where upon opening it in the browser, it only shows a blank white page. I am puzzled as to why this is happening. Could you please take a look at this link and assist me? Thank you in advance. ...

methods for collapsing a div panel using jquery

When I click outside of the expanded div, I want it to close. This is the HTML code I have: <div id="panel"> <div> <h1>Welcome to jquery demo</h1> <h3>Welcome to jquery demo</h3> ...

What is the best way to adjust the size of a background image on Safari

Using Chrome When using Chrome, the functionality is just like what's depicted in the image above. However, when using Safari The issue arises, as it does not work as expected in Safari. See below for the CSS code: background-image: url(r ...

How can I make sure that an asp:content or div element remains visible at the top when scrolling?

Within my asp:content tag, there is a div element: <asp:Content ID="btnContent" ContentPlaceHolderID="ButtonContent" runat="server"> <div id="dButtons" runat="server" visible="false"> Some CONTENT </div> </asp:Content> As ...

Utilizing CSS to style text on a menu by applying a specific class to an unordered list element

Is there a method to use CSS to invoke a div id function that will highlight text when the mouse hovers over it? <ul class="sub-menu"> <li id="menu-item-1721" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1721">< ...

Creating separation between a dropdown menu and its corresponding button

I'm dealing with a situation where I have a button that reveals a hidden droplist upon hover. The problem is that there is no space between the button and the droplist, causing interference with the functionality. My attempt to create some distance b ...

Resizing images in HTML can sometimes lead to extra white space appearing underneath the

I am facing an unusual scaling issue with my website design. Whenever I resize the browser window horizontally, the image scales correctly, but it leaves a white space below it where the rest of the page should start from if the window is large. I'm ...

When CSS media queries are applied, the background color of Div does not fully extend to the edge

Although the problem seems simple, I am finding it difficult to find a solution. This page was created as part of my course. https://i.sstatic.net/DKCva.jpg While in developer mode and trying to resize the screen, I noticed that the background color of ...

What is the best way to change the height of a div element as the user scrolls using JavaScript exclusively?

Coding with JavaScript var changeHeight = () => { let flag = 0; while(flag != 1) { size += 1; return size; } if(size == window.innerHeight/2){ flag == 1; } } var size = 5; document.body.style.height = &qu ...

Move a single <div> element according to the screen resolution

I'm a beginner in coding and struggling with a specific problem. I need to adjust the position of a container based on screen size. For large and mid-sized screens, the container needs to be left-justified with a small amount of padding from the left ...

The webpage appears to be operating smoothly when tested locally and on JSFiddle, however it is encountering

Just joined and brand new to this game. Completed a course at work over the last few weeks, and I must say, I'm really enjoying it. However, when I tried to create a page using Bootstrap styling and added an accordion and a carousel, both features fai ...

Having difficulty animating the height transition of the NextJS Image component

On my webpage, I have a headerbar that changes size (from 120px to 70px) when the user scrolls down. I successfully implemented this transition using Tailwind classes and a height transition. Now, I am trying to make the site logo resize along with the hea ...

Tips for animating slider elements that are not set to absolute positioning

I'm currently working on implementing an animated slider for my website. I have been using floats and margins to position the elements of the slider, but now I want to find a way to incorporate animations without having to use CSS3 transitions with ab ...

Expand the size of the bullets in unordered lists

What is the best way to customize UL list bullets to have a larger width without using custom images? ...

Webpage Text Animation

This is the visual representation of my webpage: https://i.stack.imgur.com/tYq34.png I am seeking to implement a uniform animation effect for the text "Carlos Qiano" and "Lorem Ipsum", similar to the link below: https://i.stack.imgur.com/XVnBS.jpg Note: ...

Locate the image in the table by searching for the alt attribute and then show the entire row

I am looking to use the alt attribute of images in a table to search and display the entire row using javascript or jquery. <div class="input-group"> <span class="input-group-addon">Filter</span> <input id=& ...

Is there a way to pause the slideshow? Are there any ways I can enhance the code to make it more efficient?

Currently, I am testing a slideshow that automatically transitions every 1 second. I have set it up to pause when it reaches the first image. However, when it stops on the initial image and I click the next button, nothing happens. If I try to interact wit ...

Is there a way to have only the <a> tag be clickable in an unordered list, without making the entire list item clickable?

I just need the text to be made clickable instead of the entire list item block. <a class="menu_head">Hello</a> <ul class="menu_body"> <li><a href="#">Sub-menu 1</a></li> <li><a href="#">Sub-menu 2 ...

The Bootstrap-Select dropdown refuses to open

I've been attempting to create a menu using data-subtext, and while I have come across some suggestions on stackoverflow, I'm still having trouble getting the menu to function properly. The issue I'm facing is that the menu doesn't seem ...