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 8869071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:23:48+00:00 2026-06-14T17:23:48+00:00

I need to do something similar to JQuery Sync Selected Radio Button between 2

  • 0

I need to do something similar to JQuery Sync Selected Radio Button between 2 radio button groups.

I have 2 radio groups –

<input type="radio" name="radio_A" id="radio_A_1" value="1" />1st
<input type="radio" name="radio_A" id="radio_A_2" value="2" />2nd 

<input type="radio" name="radio_B" id="radio_B_1" value="1" />1st
<input type="radio" name="radio_B" id="radio_B_2" value="2" />2nd

When radio_A_1 is checked, I need radio_B_2 synced/checked. The relationships would be-
radio_A_1=>radio_B_2
radio_A_2=>radio_B_1
radio_B_1=>radio_A_2
radio_B_2=>radio_A_1

Using the answer provided #8804502

$('input[name=radio_A]').change(function() {
var index = $(this).index('input[name=radio_A]');
console.log(index);
$('input[name=radio_B]:eq(' + index + ')').attr('checked','checked');
});

I get same=>same, but only when A changes-
radio_A_1=>radio_B_1
radio_A_2=>radio_B_2

So if I copy it, changing it if from A=>B to B=>A–

$('input[name=radio_A]').change(function() {
var index = $(this).index('input[name=radio_A]');
console.log(index);
$('input[name=radio_B]:eq(' + index + ')').attr('checked','checked');
});
$('input[name=radio_B]').change(function() {
var index = $(this).index('input[name=radio_B]');
console.log(index);
$('input[name=radio_A]:eq(' + index + ')').attr('checked','checked');
});

I get same=>same, when either A or B changes –
radio_A_1=>radio_B_1
radio_A_2=>radio_B_2
radio_B_1=>radio_A_1
radio_B_2=>radio_A_2

How can I sync them 1=>2/2=>1, instead of 1=>1/2=>2? And can it be done with 1 block of code, instead of 2?

  • 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-14T17:23:49+00:00Added an answer on June 14, 2026 at 5:23 pm

    You need to add some code to take the index of the element just clicked and set it to 0 if it is 1, or 1 if it is 0. The first way that came to mind is as follows:

    $('input[name=radio_A]').change(function() {
        var index = $(this).index('input[name=radio_A]') === 1 ? 0 : 1;
        $('input[name=radio_B]:eq(' + index + ')').attr('checked', 'checked');
    });
    $('input[name=radio_B]').change(function() {
        var index = $(this).index('input[name=radio_B]') === 1 ? 0 : 1;
        $('input[name=radio_A]:eq(' + index + ')').attr('checked', 'checked');
    });
    

    Demo: http://jsfiddle.net/cTQeJ/1/

    Though you could also try:

    var index = 1 - $(this).index('input[name=radio_A]');
    

    (Or the following demo uses kind of a fun hack if you like to make your code confusing: http://jsfiddle.net/cTQeJ/)

    “And can it be done with 1 block of code, instead of 2?”

    If you can modify the html slightly it is pretty easy with a single, short code block. Try a change like this:

    <input type="radio" name="radio_A" data-sync="1" value="1" />1st
    <input type="radio" name="radio_A" data-sync="2" value="2" />2nd
    
    <input type="radio" name="radio_B" data-sync="2" value="1" />1st
    <input type="radio" name="radio_B" data-sync="1" value="2" />2nd
    

    I’ve added a data-sync attribute to each radio button (and removed the id attribute since it wasn’t being used, but obviously you can leave that in if needed). Then you can write a simple function like this:

    var $radios = $('input[data-sync]');
    $radios.change(function() {
        $radios.filter('[data-sync="' + $(this).attr('data-sync') + '"]')
               .prop('checked', true);
    });​
    

    …that basically says whenever any of the radio buttons is checked, find all other radio buttons with a data-sync attribute of the same value and check them too.

    Note that this will work with more than two groups, as shown in this demo: http://jsfiddle.net/cTQeJ/2/

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

Sidebar

Related Questions

I want something similar to Mysql's sorting of data, but in jquery. I have
I need to do something similar to this: jQuery('#ImgCarousel').jcarousel({}); jQuery('#ImgCarouselCon').hide(); ImgCarouselCon is the container
I need something similar to this -> How to show loading spinner in jQuery?
I have something similar to this: <!doctype html> <html> <head> <meta charset=UTF-8> <title>JQuery Test</title>
I excavated this thread: JQuery live or something similar with .change()? I have exactly
Is there a URL encoding function in XSLT version 1? I need something similar
I need to build a Windows control that displays KPI indicators. Something similar to
I need something simple; I have page where a user clicks an author to
I need something like this http://jonraasch.com/blog/a-simple-jquery-slideshow but w/o the absolute positioning. Is it possible?
Does anyone know of an onHide() event or something similar in jQuery? I tried:

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.