Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML radio buttons allowing multiple selections

Tags:

html

forms

In my HTML form I have the below as a set of radio buttons, depending on what radio button you select depends on what the next form <fieldset> is revealed, this all works. The problem is for some reason they are working like a check box and not as a radio button. So you can select all options and not just the one at a time.

Can anyone see where this is going wrong in the code below?

  <fieldset>         <legend>Please select one of the following</legend>         <input type="radio" name="track" id="track" value="track" /><label for="track">Track Submission</label><br />         <input type="radio" name="event" id="event" value="event"  /><label for="event">Events and Artist booking</label><br />         <input type="radio" name="message" id="message" value="message" /><label for="message">Message us</label><br />   </fieldset> 
like image 672
SamesJeabrook Avatar asked Jan 07 '14 22:01

SamesJeabrook


People also ask

Can radio buttons have multiple selections?

Radio buttons allow a user to select a single option among multiple options. You can set the Choice Value of each option, for each button, as well as group these buttons by giving them the same Group Name.

How do I enable multiple selected radio buttons in HTML?

Check boxes allow multiple selections at a time and radio buttons allow only a single selection at a time, but both use the <input> tag to create each box or button.

How many radio buttons can be selected at once in HTML?

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?

Only one radio button in a group can be checked. You have two radio buttons with different names. 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.


2 Answers

They all need to have the same name attribute.

The radio buttons are grouped by the name attribute. Here's an example:

<fieldset>     <legend>Please select one of the following</legend>     <input type="radio" name="action" id="track" value="track" /><label for="track">Track Submission</label><br />     <input type="radio" name="action" id="event" value="event"  /><label for="event">Events and Artist booking</label><br />     <input type="radio" name="action" id="message" value="message" /><label for="message">Message us</label><br /> </fieldset> 
like image 173
lukasgeiter Avatar answered Sep 19 '22 11:09

lukasgeiter


I've done it this way in the past, JsFiddle:

CSS:

.radio-option {     cursor: pointer;     height: 23px;     width: 23px;     background: url(../images/checkbox2.png) no-repeat 0px 0px; }  .radio-option.click {     background: url(../images/checkbox1.png) no-repeat 0px 0px; } 

HTML:

<li><div class="radio-option"></div></li> <li><div class="radio-option"></div></li> <li><div class="radio-option"></div></li> <li><div class="radio-option"></div></li> <li><div class="radio-option"></div></li> 

jQuery:

<script>     $(document).ready(function() {         $('.radio-option').click(function () {             $(this).not(this).removeClass('click');             $(this).toggleClass("click");         });     }); </script> 
like image 43
Todd Williams Avatar answered Sep 19 '22 11:09

Todd Williams