Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to select one radio button at a time

Tags:

html

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 &nbsp Option A <br>
                <input type="radio" name="B" value="b" id="radio2"> B &nbsp Option B <br>
                <input type="radio" name="C" value="c" id="radio3"> C &nbsp Option C <br>
                <input type="radio" name="D" value="d" id="radio4"> D &nbsp Option D <br>
                <br>
                &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
                <input type="button" name="ans1" value="Next" onclick="answer_a()">
            </td>
        </tr>
    </table>
</form>
like image 752
Sherry Avatar asked Sep 14 '13 07:09

Sherry


People also ask

How many radio buttons you can select at a time from one group?

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.

Why can I select multiple radio buttons?

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.

How can I avoid multiple selected radio buttons?

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.


2 Answers

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 &nbsp Option A <br>
<input type="radio" name="A" value="b" id="radio2"> B &nbsp Option B <br>
<input type="radio" name="A" value="c" id="radio3"> C &nbsp Option C <br>
<input type="radio" name="A" value="d" id="radio4"> D &nbsp Option D <br>
like image 142
Dipesh Parmar Avatar answered Oct 10 '22 01:10

Dipesh Parmar


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 &nbsp Option A <br>
<input type="radio" name="options" value="b" id="radio2"> B &nbsp Option B <br>
<input type="radio" name="options" value="c" id="radio3"> C &nbsp Option C <br>
<input type="radio" name="options" value="d" id="radio4"> D &nbsp Option D <br>
like image 3
Zero Fiber Avatar answered Oct 10 '22 03:10

Zero Fiber