Radio button is not working properly
Asked Answered
P

9

35

In my web page I have placed some radio buttons. But these buttons are not working properly. I can check multiple buttons.

code:

<label for="abc" style="margin-top:-20px;margin-left:40px">xyz</label>
<input type="radio" id="abc" name="abc" >        
<label for="bcd" style="margin-top:-20px;margin-left:40px">abc</label>
<input type="radio" id="bcd" name="bcd" >
<label for="efg" style="margin-top:-20px;margin-left:40px">ccc</label>
<input type="radio" id="efg" name="efg" >

fiddle

I want to check only one button. Please any one help me.

Pasadis answered 31/10, 2013 at 15:37 Comment(0)
R
87

Because you've different value for name attribute, they should have a common name value, just like you group items.. for example

<input type="radio" name="group1" />
<input type="radio" name="group1" />
<input type="radio" name="group1" />

<!-- You can select any one from each group -->

<input type="radio" name="group2" />
<input type="radio" name="group2" />
<input type="radio" name="group2" />

Demo

Rattlehead answered 31/10, 2013 at 15:38 Comment(1)
Ah man, this saved me some serious frustration. Some times it's good to go back to the fundamentals! Thank you.Baht
R
6
<label for="abc" style="margin-top:-20px;margin-left:40px">xyz</label>
<input type="radio" id="abc" name="abc" >        
<label for="bcd" style="margin-top:-20px;margin-left:40px">abc</label>
<input type="radio" id="bcd" name="abc" >
<label for="efg" style="margin-top:-20px;margin-left:40px">ccc</label>
<input type="radio" id="efg" name="abc" >

All inputs must have the same name="" attribute value

Rubber answered 31/10, 2013 at 15:40 Comment(0)
C
4

Radio buttons which are grouped together must have the same case-sensitive name property.

<label for="input1">First Input</label>
<input type="radio" id="input1" name="inputGroup" >
<label for="input2">Second Input</label>
<input type="radio" id="input2" name="inputGroup" >
<label for="input3">Third Input</label>
<input type="radio" id="input3" name="inputGroup" >

JSFiddle demo.

From the HTML specification:

Radio buttons are like checkboxes except that when several share the same control name, they are mutually exclusive.

Cole answered 31/10, 2013 at 15:39 Comment(0)
E
2

I reached this thread searching the keywords "html radio not working". After reviewing my code, I noticed that I used a javascript method "event.preventDefault();" in this custom function that I've made where the HTML node that triggered this event in that custom function were the "not working" radio parent. So I solved my problem removing the "event.preventDefault();". If someone reached this thread in the future, I hope this help you somehow (even for debbuging purposes).

Eliason answered 3/2, 2022 at 14:24 Comment(0)
G
1

Give same name to all radio button from which you want to select one option

<label for="abc" style="margin-top:-20px;margin-left:40px">xyz</label>
<input type="radio" id="abc" name="abc" >        
<label for="bcd" style="margin-top:-20px;margin-left:40px">abc</label>
<input type="radio" id="bcd" name="abc" >
<label for="efg" style="margin-top:-20px;margin-left:40px">ccc</label>
<input type="radio" id="efg" name="abc" >

Now it will work properly

Gildus answered 18/12, 2018 at 12:57 Comment(0)
C
0

Name attribute needs to be the same. Name groups radio buttons together to make them one unit.

Cort answered 31/10, 2013 at 15:38 Comment(0)
B
0

Name them the same way, and in your php or receiving code it will be something like

$_POST['name'] = 'value of selected radio button'
Beckiebeckley answered 31/10, 2013 at 15:40 Comment(0)
M
0

The name setting tells which group of radio buttons the field belongs to. When you select one button, all other buttons in the same group are unselected. If you couldn't define which group the current button belongs to, you could only have one group of radio buttons on each page. e.g :

<input type="radio" name="fruit1" value="Apple"> Apple <br>
<input type="radio" name="fruit1" value="Apricot" checked> Apricot <br>
<input type="radio" name="fruit1" value="Avocado"> Avocado
<hr>
<input type="radio" name="fruit2" value="Banana"> Banana<br>
<input type="radio" name="fruit2" value="Breadfruit"> Breadfruit<br>
<input type="radio" name="fruit2" value="Bilberry" checked>  Bilberry
Mentor answered 11/9, 2014 at 5:50 Comment(0)
S
0

Give the name the same attribute. The checked attribute can be used to indicate which value should be selected. Somewhere in your syntax for the value you want checked write checked="checked"

Shrewmouse answered 18/11, 2019 at 4:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.