Show two divs when a radio button is chosen

How can I make it so that when a radio option is selected, two DIVs are displayed? Here's my HTML:

/* Here is the CSS */

    #firstblock {
           display: none;
           padding: 10px;
    }    
    #secondblock {
           display: none;
           padding: 10px;
    }
    #uploadButton {
           display: none;
           padding: 10px;
    }
    input[value="firstblock"]:checked ~ #firstblock {
            display: block;
            #uploadbutton: display: block;
    }
    
    input[value="secondblock"]:checked ~ #secondblock {
          display: block;
    }
<form id = "selectFeature" method = "POST">
      <input type="radio" name="action" value="firstblock" /> First Upload
      <input type="radio" name="action" value="secondblock" /> Second Upload
    
    <div id ="firstblock" class="show-hide">
    <h2> This is the first block. <br> </h2>
    </div>
                                      
    <div id="secondblock" class="show-hide">
      <h2> This is the second block </h2>
      </div>
      
    <div id="uploadButton">
      This is a button
    </div>
</form>

Is there an easy way to only show the upload button after selecting either the firstblock or secondblock option?

Thanks in advance.

Answer №1

To achieve the desired result, simply replicate what you have already done.

input[value="firstblock"]:checked ~ #uploadButton    
input[value="secondblock"]:checked ~ #uploadButton

Below is a snippet of the code:

#firstblock {
       display: none;
       padding: 10px;
}   
 
#secondblock {
       display: none;
       padding: 10px;
}

#uploadButton {
       display: none;
       padding: 10px;
}

input[value="firstblock"]:checked ~ #firstblock,
input[value="firstblock"]:checked ~ #uploadButton {
      display: block;
}

input[value="secondblock"]:checked ~ #secondblock,
input[value="secondblock"]:checked ~ #uploadButton {
      display: block;
}
<form id = "selectFeature" method = "POST">
  <input type="radio" name="action" value="firstblock" /> First Upload
  <input type="radio" name="action" value="secondblock" /> Second Upload

<div id ="firstblock" class="show-hide">
<h2> Following is first block. <br> </h2>
</div>

<div id="secondblock" class="show-hide">
  <h2> Following is second block </h2>
  </div>

<div id="uploadButton">
  This is button
</div>
</form>


Here are some suggestions for improvement:

If you wish to apply the same rule to multiple ids or classes, consider minifying your css.

Minified version would look like this:

#firstblock,
#secondblock,
#uploadButton {
   display: none;
   padding: 10px;
}

input[value="firstblock"]:checked ~ #firstblock,
input[value="firstblock"]:checked ~ #uploadButton,
input[value="secondblock"]:checked ~ #secondblock,
input[value="secondblock"]:checked ~ #uploadButton {
      display: block;
}

Add labels to your inputs to allow users to select them by clicking on the text.

Example with labels:

<label>
  <input type="radio" name="action" value="firstblock" id="firstradio"/>
  First Upload
</label>
<label>
  <input type="radio" name="action" value="secondblock" id="secondradio" />
  Second Upload
</label>

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

Implement a Selection Feature in Angular

Currently, I am working on an application where users can add new rows with the same fields. One of the requirements is to allow users to add an option to a select element. While I have successfully implemented this in jQuery, I am facing challenges integr ...

Picture appears to be off-center

I created a loginPage.php file with the following content: <?php //some php stuff ?> <!DOCTYPE html> <html> <head> <title>Login Form Design</title> <link rel="stylesheet" type="text/css" href="stylelogin.c ...

Slide-out left menu using HTML and CSS

Currently, I am in the process of constructing a website that requires a menu to gracefully slide from the left side of the screen (with a width of 0px) to the right side (expanding to a width of 250px), all with the help of jQuery animations. Here is wha ...

Incorporate a Captcha image into your Laravel application without the need for any additional packages

After successfully uploading a laravel app, my boss requested that I add a captcha image to the website. However, all the captcha techniques for Laravel I've researched require installing a package, and I'm hesitant to reupload the entire website ...

Increment button value by one using JavaScript or jQuery

I am trying to create a button that increments its value with each click, which I will then need to submit to my database. Currently, my implementation causes the incremented values to display properly, but the button text "GOAL" disappears when clicked. ...

What could be causing my select tags to appear incorrectly in Firefox and IE?

With the help of Jquery, my goal is to dynamically populate a select field when it is in focus, even if it already has a value. Once populated, I want to retain the previous value if it exists as an option in the newly populated field. Although this works ...

The HTML link is not directly attached to the text, specifically in Internet Explorer

When viewing my website in Chrome, the menu links in the Header function correctly. However, in Internet Explorer (specifically version 11), hovering over 'HOME,' 'LISTINGS,' or 'AGENTS' reveals that the displayed URL is incor ...

What steps can I take to ensure that a user only clicks on my voting link once for a specific post?

I am managing a MySQL database with two tables. One table is named 'users' with the column 'uname' as the primary key, which stores usernames as strings. The second table is named 'posts' with the column 'id' as the ...

The inline display property does not function properly with the <li> element

Here is the HTML code snippet I am working with: <div class="nav"> <ul class="nav-dots"> <li class='ni n1'><a href="" class='nia'>dot</a></li> <li class='ni n2'><a href="" clas ...

Creating a dynamic workspace with a portable viewing window

I am looking to create a spacious workspace for arranging objects, but I also want to incorporate a customizable "viewport" that will display a specific area of the workspace for the user. This viewport should be resizable and movable within the workspace, ...

Utilizing the transform: origin property to perfectly align an element

I've tilted a basic paragraph and I'm attempting to position it on the left side of the browser at 0% from the top of the browser at 50%. http://example.com p { position: fixed; -webkit-transform-origin: left center; -webkit-transfo ...

Having trouble with CSS selectors functioning properly in Rhomobile Studio for Android

I am a beginner in the world of Rhodes and Rhomobile Studio. Coming from a Rails background, I am used to the .css.scss file extensions which allow powerful CSS3 codes to be executed. However, in Rhomobile Studio, these extensions are not recognized. I am ...

Retrieve the Vue.js JavaScript file from the designated static directory

This is my inaugural attempt at creating a web application, so I am venturing into Vue.js Javascript programming as a newcomer. I have chosen to work with the Beagle Bootstrap template. Within my Static folder, I have a file named app-charts-morris.js whi ...

Modifying the color of the highlighted selection in a dropdown select box

Is it possible to change the blue highlight on this dropdown to gray? Check out this demo of a select box I am aiming to alter the highlight color to gray, can someone help me achieve this? select { border: 0; color: #EEE; background: transparen ...

Scrolling automatically

I'm experimenting with creating a typing effect using JavaScript and the typed.js library, found here: My approach involves using a div as a container. However, I've encountered an issue - when the text reaches the height of the div, scroll bars ...

The elusive Holy Grail layout for Flex content cannot be achieved on IE11

UPDATE I attempted to insert height:100vh into the .app-container, but unfortunately, it did not have the desired effect. I am utilizing bootstrap 4 for creating my web application. My goal is to implement the Holy grail layout. Everything appears to be ...

Create a sleek Bootstrap 4 webpage featuring a fixed footer and navigation bar. However, the challenge lies in ensuring that the content fills the

Currently, I am in the process of creating a template page that includes a navbar and a footer with the intention of adding additional columns within the main div (container-fluid) at a later stage. However, I have encountered two issues that I cannot seem ...

Discover the process of retrieving HTML elements from a different webpage and incorporating them into your own site

As a newcomer, I'm in search of a code that can help me fetch an HTML string from one webpage and use it in another webpage. To illustrate my point, consider the following mock examples. Example 1: Mysite.com/A.html <body> <!---My Script Goe ...

Is there a way to direct to a particular section of an external website without relying on an id attribute in the anchor tag?

I am aware that you can link to specific id attributes by using the following code: <a href="http://www.external-website.com/page#some-id">Link</a> However, what if the external HTML document does not have any ids to target? It seems odd tha ...

Tips on aligning multiple elements vertically in a justified manner

I'm struggling to articulate what I need, but I'll give it a shot. I want to vertically align three different elements, each wrapped in their own divs. This is my current code: .service_info { margin-top: 45px; clear: both; ...