I am unable to select one radio button at a time. Multiple buttons are getting selected. I am newbie to html. This is my code. Please help.
<form name="ans_a" onsubmit="return answer_a()">
<table align="center" border="1">
<br>
<br>
<tr>
<td width="500px"> ABC
<br>
<input type="radio" name="A" value="a" id="radio1"> A   Option A <br>
<input type="radio" name="B" value="b" id="radio2"> B   Option B <br>
<input type="radio" name="C" value="c" id="radio3"> C   Option C <br>
<input type="radio" name="D" value="d" id="radio4"> D   Option D <br>
<br>
                 
<input type="button" name="ans1" value="Next" onclick="answer_a()">
</td>
</tr>
</table>
</form>
Only one radio button in a given group can be selected at the same time. Radio buttons are typically rendered as small circles, which are filled or highlighted when selected.
This means that you have two radio groups, each containing one radio button. You need to put them in the same group (by making them share a name) if you only want one of them to be selected.
As per my understanding you want to group the radio buttons such that one is selected at a time you can do it by adding name attribute in each radio button and give same name to all of them. So at time of submit you can get the value of selected variable in submit function call.
The name attribute must be the same to select only one radio button
at a time. The id could be different depending on if you want to capture that somewhere which one of the radio buttons is selected.
<input type="radio" name="A" value="a" id="radio1"> A   Option A <br>
<input type="radio" name="A" value="b" id="radio2"> B   Option B <br>
<input type="radio" name="A" value="c" id="radio3"> C   Option C <br>
<input type="radio" name="A" value="d" id="radio4"> D   Option D <br>
To select only one radio button, you have to put them under one group, i.e make the name attribute same for all of them.
<input type="radio" name="options" value="a" id="radio1"> A   Option A <br>
<input type="radio" name="options" value="b" id="radio2"> B   Option B <br>
<input type="radio" name="options" value="c" id="radio3"> C   Option C <br>
<input type="radio" name="options" value="d" id="radio4"> D   Option D <br>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With