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

  • SEARCH
  • Home
  • 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 7835347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:36:08+00:00 2026-06-02T13:36:08+00:00

I have two radio buttons to toggle two alternative texts and it works: <!DOCTYPE

  • 0

I have two radio buttons to toggle two alternative texts and it works:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
            type="text/javascript"></script>
    <style type="text/css">
        .displayNone { display: none; }
    </style>
</head>

<body>

    <input type="radio" name="toggle" />A
    <input type="radio" name="toggle" />B

    <span class="A">A</span>
    <span class="B displayNone">B</span>

<script type="text/javascript">
    $('input[name=toggle]').change(function() {
        $('.A, .B').toggleClass('displayNone');
    })
</script>

</body>
</html>

Now I want to make it three radio buttons to toggle between three texts:

<input type="radio" name="toggle" />A
<input type="radio" name="toggle" />B
<input type="radio" name="toggle" />C

<span class="A">A</span>
<span class="B displayNone">B</span>
<span class="C displayNone">C</span>

The solution I’m thinking about looks too verbose. What would be the clean one?

  • 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-02T13:36:09+00:00Added an answer on June 2, 2026 at 1:36 pm

    One further approach:

    $('input:radio[name="toggle"]').change(
        function(){
            $('span')
                .eq($(this).index())
                .removeClass('displayNone')
                .siblings('span')
                .addClass('displayNone');
        });​
    

    JS Fiddle demo.


    Edited to add, for the more up-to-date browsers, a pure CSS means of accomplishing the same:

    input[type=radio]:nth-child(1):checked ~ span:nth-of-type(1),
    input[type=radio]:nth-child(2):checked ~ span:nth-of-type(2),
    input[type=radio]:nth-child(3):checked ~ span:nth-of-type(3){
        height: 1em;
        width: 100%;
        background-color: #f90;
        -webkit-transition: all 1s linear;
        -ms-transition: all 1s linear;
        -o-transition: all 1s linear;
        -moz-transition: all 1s linear;
        transition: all 1s linear;
    }
    
    span {
        display: block;
        overflow: hidden;
        height: 0;
        width: 0;
        background-color: #fff;
        -webkit-transition: all 1s linear;
        -ms-transition: all 1s linear;
        -o-transition: all 1s linear;
        -moz-transition: all 1s linear;
        transition: all 1s linear;
    }​
    

    JS Fiddle demo.

    jQuery References:

    • addClass().
    • change().
    • eq().
    • index().
    • :radio selector.
    • removeClass().
    • siblings().

    CSS References:

    • CSS transitions.
    • E ~ F general-sibling combinator.
    • :nth-child() pseudo-class.
    • :nth-child() W3.org wiki entry.
    • :nth-of-type() pseudo-class.
    • :nth-of-type() W3.org wiki entry.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have those two html radio buttons and textarea. I basically want to enable
I have two radio buttons on my MVC form that I use to hide
I have two radio buttons next to each other, and even though each one
I have two radio buttons each with a unique ID. When one is clicked,
I have a table with two radio buttons for each row, these buttons are
In main.xml I would like to have a spinner1 with two radio buttons and
I have a radio group defined with two buttons for black and color. When
I have two radio buttons and I want to reset all the validation of
I have two radio buttons in one group, I want to check the radio
I have two radio buttons, to which I have assigned change handlers to show/hide

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.