How to identify which radio button is accepted?
Asked Answered
A

4

5

How can I identify which radio button is accepted?

How do I prevent a user being able to select multiple options when I use radio buttons?

<input type='radio' name='one'>option1<br />
<input type='radio' name='two'>option2<br />
<input type='radio' name='three'>option3<br />
<input type='radio' name='four'>option4<br />
<input type='radio' name='five'>option5<br />

Thats the code I use, thanks for reading.

Anselmi answered 4/7, 2010 at 20:25 Comment(0)
W
26

You need to use the "name" attribute to tie them all together, use the value attribute to give them different values.

<input type="radio" name="number" value="one" /> One<br />
<input type="radio" name="number" value="two" /> Two<br />
<input type="radio" name="number" value="three" /> Three<br />

Think of name as last name... For example, if you had Bart, Lisa, Homer, Marge and Maggie, the name would be their last name: Simpson, the value would be the said names:

<input type="radio" name="Simpson" value="Bart" /> Bart<br />
<input type="radio" name="Simpson" value="Lisa" /> Lisa<br />
<input type="radio" name="Simpson" value="Marge" /> Marge<br />
<input type="radio" name="Simpson" value="Homer" /> Homer<br />
<input type="radio" name="Simpson" value="Maggie" /> Maggie<br />
Wilsonwilt answered 4/7, 2010 at 20:27 Comment(3)
Great answer, very useful. I will give this the green tick as soon as it allows me. Thanks bud.Anselmi
So what if i need to know which button was selected? Is that possible?Anselmi
The value will be the one which was selected.Narcolepsy
L
4

you should give them the same name

Libenson answered 4/7, 2010 at 20:26 Comment(0)
S
0

Also the order of name/value pair matter's I guess.Name should appear before the order

Selaginella answered 6/8, 2012 at 10:4 Comment(0)
E
0

I know! Do like this:

<label><input type="radio" name="a">1</label>
<label><input type="radio" name="a">2</label>
<label><input type="radio" name="a">3</label>

Give everything same value for 'name' attribute. You add the name for the radio buttons

Elyn answered 28/1, 2014 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.