1

I have groups of radio buttons at different levels with same class in each level:

<div> <!-- radio group one -->
  <input type="radio" name="rdo1" class="some1 text4 radio_group_1" />
  <input type="radio" name="rdo2" class="some2 text5 radio_group_1" />
  <input type="radio" name="rdo3" class="some3 text6 radio_group_1" />
</div>
<div> <!-- radio group two -->
  <input type="radio" name="rdo4" class="some1 text4 radio_group_2 other1" />
  <input type="radio" name="rdo5" class="some2 text5 radio_group_2 other2" />
  <input type="radio" name="rdo6" class="some3 text6 radio_group_2 other3" />
</div>
... <!-- radio group three (and so on) -->

I can't change the radio names (due to server limitations). So the only thing that I have is the classes that starts with 'radio_group_'. I use this solution and this without success because the class names are in the middle of other class names.

How can I select radios with same class name and make them act like group radios?

Community
  • 1
  • 1
Vahid
  • 3,384
  • 2
  • 35
  • 69

4 Answers4

1

Do you want such behavior:

$(document).ready(function() {
  $("div > input[type=radio]").click(function() {

    var thisParent = $(this).closest("div");
    var prevClicked = thisParent.find(":checked");
    var currentObj = $(this);
    prevClicked.each(function() {
      if (!$(currentObj).is($(this))) {
        $(this).prop("checked", false);
      }
    });

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
Group 1
<div>
  <!-- radio group one -->
  <input type="radio" name="rdo1" class="some1 text4 radio_group_1" />rdo1
  <input type="radio" name="rdo2" class="some2 text5 radio_group_1" />rdo2
  <input type="radio" name="rdo3" class="some3 text6 radio_group_1" />rdo3
</div>
<br/>Group 2
<div>
  <!-- radio group two -->
  <input type="radio" name="rdo4" class="some1 text4 radio_group_2 other1" />rdo4
  <input type="radio" name="rdo5" class="some2 text5 radio_group_2 other2" />rdo5
  <input type="radio" name="rdo6" class="some3 text6 radio_group_2 other3" />rdo6
</div>
vijayP
  • 11,432
  • 5
  • 25
  • 40
1
<div id="radio_group_1">
  <!-- radio group one -->
  <input type="radio" name="rdo1" class="some1 text4 radio_group_1" />rdo1
  <input type="radio" name="rdo2" class="some2 text5 radio_group_1" />rdo2
  <input type="radio" name="rdo3" class="some3 text6 radio_group_1" />rdo3
</div>
<div id="radio_group_2">
  <!-- radio group two -->
  <input type="radio" name="rdo4" class="some1 text4 radio_group_2 other1" />rdo4
  <input type="radio" name="rdo5" class="some2 text5 radio_group_2 other2" />rdo5
  <input type="radio" name="rdo6" class="some3 text6 radio_group_2 other3" />rdo6
</div>

$("input[type=radio]").on("click", function(){
 var groupName = $(this).closest("div").attr("id");
   $("input[type=radio]."+groupName).prop("checked", false);
   $(this).prop("checked", true);
});

https://jsfiddle.net/tr73o3e6/2/

Sagar
  • 259
  • 2
  • 3
  • 14
0

I don't know why you said:

I can't change the radio names

Anyway, The answer to what you're asking maybe like this:

$('.radio_group_1, .radio_group_2').click(function(){
  
  $(this).prop('checked', true).siblings().prop('checked', false);
  
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> <!-- radio group one -->
  <input type="radio" name="rdo1" class="some1 text4 radio_group_1" />
  <input type="radio" name="rdo2" class="some2 text5 radio_group_1" />
  <input type="radio" name="rdo3" class="some3 text6 radio_group_1" />
</div>
<div> <!-- radio group two -->
  <input type="radio" name="rdo4" class="some1 text4 radio_group_2 other1" />
  <input type="radio" name="rdo5" class="some2 text5 radio_group_2 other2" />
  <input type="radio" name="rdo6" class="some3 text6 radio_group_2 other3" />
</div>
rmondesilva
  • 1,732
  • 3
  • 17
  • 29
0
$("input[type=radio]").click(function () {
    var thisClass = $(this).attr("class");
    console.log(thisClass);

    $('.'+thisClass).each(function () {
        $(this).prop("checked", false);
    });
    $(this).prop("checked", true);
});

You have multiple classes so select class you want to pass to each and it will work fine

nikunjM
  • 560
  • 1
  • 7
  • 21