Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8948411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:58:51+00:00 2026-06-15T12:58:51+00:00

I’ve made a form to process user input using PHP , and the first

  • 0

I’ve made a form to process user input using PHP, and the first question asks the user to select one of three radio buttons to fill in the “type” parameter – the available options are “book”, “journal”, and “website”, and the code looks like this:

<strong>Type of work:</strong>
<input type="radio" name="type" id="book" value="book" checked="checked" /> <label for="book">Book</label>
<input type="radio" name="type" id="journal" value="journal" /> <label for="journal">Journal</label>
<input type="radio" name="type" id="website" value="website" /> <label for="website">Website</label>

Further down the page, I have three fieldsets (using <fieldset>), each corresponding to one of the types. I’d like to have only one of these display at a time, depending on which radio button is selected, to make the page look cleaner.

Unfortunately, I’m a total JavaScript noob, and my last attempt broke things really badly. The fieldsets already have IDs (boxBook, boxJournal, and boxWebsite), although they currently don’t do anything special.

If it affects anything, I’d like the output to be valid HTML5, and to degrade gracefully, displaying all three fieldsets if the user has JS disabled.

Any help would be greatly appreciated ^^

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:58:52+00:00Added an answer on June 15, 2026 at 12:58 pm

    Using jQuery I’d suggest:

    // hides the elements using jQuery (so they're visible without JavaScript)
    $('#boxBook, #boxJournal, #boxWebsite').hide();
    
    // when the radio inputs whose name is equal to "type" is changed:
    $('input:radio[name="type"]').change(function() {
        var id = this.id;
    
        // hides all the fieldset elements whose `id` starts with "box":
        $('fieldset[id^="box"]').hide();
    
        // looks for the element with the id equal to
        // `box` + upper-cased first-letter of this.id +
        // the substring from second-letter onwards of this.id
        $('#box' + id[0].toUpperCase() + id.substring(1)).show();
    });​
    

    JS Fiddle demo.

    Incidentally, rather than performing string manipulation on the id of the radio input, it’d be easier to either use a data-* attribute to specify the precise id of the targeted element:

    <input type="radio" id="book" name="type" data-targets="boxBook" />
    

    And use:

    $('#boxBook, #boxJournal, #boxWebsite').hide();
    
    $('input:radio[name="type"]').change(function() {
        var id = $(this).attr('data-targets'); // or: $(this).data('targets');
        $('fieldset[id^="box"]').hide();
        $('#' + id).show();
    });​
    

    JS Fiddle demo.


    Edited the latter code-block to meet the OP’s requirements:

    $('input:radio[name="type"]').change(function() {
        $(this).siblings('input:radio[name="type"]').each(function() {
            $('#' + $(this).data('targets')).hide();
        });
        $('#' + $(this).data('targets')).show();
    }).filter(function() {
        return !this.checked;
    }).each(function() {
        $('#' + $(this).data('targets')).hide();
    });​
    

    JS Fiddle demo.

    Though, frankly, I think I’ve over-complicated it quite a bit. But it does work, and meets the needs specified in the comment:

    if one of the radio buttons is checked by default, the fieldset doesn’t display. I’d like to have it default to book if possible

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.