Multiple radio button groups in one form
Asked Answered
M

6

179

Is it possible to have multiple radio button groups in a single form? Usually selecting one button deselects the previous, I just need to have one of a group deselected.

<form>
    <fieldset id="group1">
        <input type="radio" value="">
        <input type="radio" value="">
    </fieldset>

    <fieldset id="group2">
        <input type="radio" value="">
        <input type="radio" value="">
        <input type="radio" value="">
    </fieldset>
</form>
Muffin answered 16/2, 2015 at 14:35 Comment(1)
Give them names (i.e. <input type="checkbox" name="checkGroup1" value =""/>Machine
S
323

Set equal name attributes to create a group;

<form>
  <fieldset id="group1">
    <input type="radio" value="value1" name="group1">
    <input type="radio" value="value2" name="group1">
  </fieldset>

  <fieldset id="group2">
    <input type="radio" value="value1" name="group2">
    <input type="radio" value="value2" name="group2">
    <input type="radio" value="value3" name="group2">
  </fieldset>
</form>
Strangeness answered 16/2, 2015 at 14:44 Comment(2)
if value equals "" every time, how do I know which radio button was chosen? how do I know if a radio button was chosen at all?Ingrained
Insert your own valuesStrangeness
R
20

This is very simple you need to keep different names of every radio input group.

      <input type="radio" name="price">Thousand<br>
      <input type="radio" name="price">Lakh<br>
      <input type="radio" name="price">Crore
      
      </br><hr>

      <input type="radio" name="gender">Male<br>
      <input type="radio" name="gender">Female<br>
      <input type="radio" name="gender">Other
Relate answered 28/3, 2019 at 11:38 Comment(0)
S
15

Just do one thing, We need to set the name property for the same types. for eg.

Try below:

<form>
    <div id="group1">
        <input type="radio" value="val1" name="group1">
        <input type="radio" value="val2" name="group1">
    </div>
</form>

And also we can do it in angular1,angular 2 or in jquery also.

<div *ngFor="let option of question.options; index as j">
<input type="radio" name="option{{j}}" value="option{{j}}" (click)="checkAnswer(j+1)">{{option}}
</div>  
Shackleton answered 26/10, 2017 at 10:18 Comment(0)
C
4

in input field make name same like

<input type="radio" name="option" value="option1">
<input type="radio" name="option" value="option2" >
<input type="radio" name="option" value="option3" >
<input type="radio" name="option" value="option3" >
Crowning answered 1/6, 2019 at 16:50 Comment(0)
S
1

Angular

For those ended up here having a similar issue with Angular, I fixed my problem by replacing the [attr.name] of the <input> with simply [name] which was the cause of the issue.

Surgy answered 7/11, 2023 at 15:33 Comment(0)
S
0

To create a group of inputs you can create a custom html element

window.customElements.define('radio-group', RadioGroup);

https://gist.github.com/robdodson/85deb2f821f9beb2ed1ce049f6a6ed47

to keep selected option in each group, you need to add name attribute to inputs in group, if you not add it then all is one group.

Struggle answered 20/2, 2019 at 11:7 Comment(1)
Can you specify how this fixes the question's issue? This also creates just one group, does it add a unique name to the inputs of each group you create this way?Extrovert

© 2022 - 2024 — McMap. All rights reserved.