skip to Main Content

I have a dynamic radio button group. The names and Ids are dynamic. The issue I am having is that I am able to select multiple radio buttons at the same time because the names are different. I need to be able to select only one at a time. I assigned a class to the group and want to be able to select only one using the class name.

Tried this but it’s not working

$('.rClass').click(function () {
    $('.rClass').prop("checked", false);
    $(this).prop("checked", true);
});

but it’s not working. Can someone please show me what I am doing wrong here?

Thanks

2

Answers


  1. To create a radio button group with jQuery and limit the user to selecting only one radio button at a time, you can use the following code:

    $('.rClass input[type="radio"]').on('click', function() {
       $('.rClass input[type="radio"]').not(this).removeAttr('checked');
       $(this).attr('checked', 'checked');
    });
    
    Login or Signup to reply.
  2. Use event delegation on the nearest static ancestor of all the radio inputs.

    $(document).on('click', '.rClass', function (){
        // ...
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search