Radio buttons in HTML




What is radio buttons?

Radio buttons are small buttons you can click to make a choice between several options. Buttons to click for choosing between options exist in two versions, radio buttons where you can only choose one option and checkboxes with no limit to the number of choices you can select.


How do you make radio buttons

When making an input field, using radio buttons, the first thing you need is a FORM tag, i.e.

<FORM>
</FORM>

Along with this, you need the tag INPUT. How this is written, depends on what you want to do. The code for radio buttons looks like this:

<FORM>
<INPUT TYPE="radio">Option 1
<INPUT TYPE="radio">Option 2
</FORM>

This is insufficient for making radio buttons work properly. INPUT needs to have the attributes NAME and VALUE specified. NAME is the name of the group of values to be chosen from. If that isn't specified, FORM can't change between the options. VALUE is the value being returned, when clicking the radio buttin/option. The code for the radio buttons now looks like this:

<FORM>
<INPUT TYPE="radio" NAME="Selection" VALUE="Option 1">Option 1
<INPUT TYPE="radio" NAME="Selection" VALUE="Option 2">Option 2
</FORM>

On the screen, it looks like this:

Option 1
Option 2


Preselected value on the list

For several reasons, it can be an advantage to have a preselected/default value on your list. For one thing, you don't need an error message for no choice if you have a script reading the choice(s). If there is a lot of values to be clicked, you can mark the most popular choices, thereby making it easier for the user, by only having to change values where needed.

The way to do it is simple. You write checked at the INPUT tag you want preselected, e.g. preselection of Option 2 of 5, as shown here:

<FORM>
<INPUT TYPE="radio" NAME="Preselection" VALUE="Option 1">Option 1
<INPUT TYPE="radio" NAME="Preselection" VALUE="Option 2" checked>Option 2
<INPUT TYPE="radio" NAME="Preselection" VALUE="Option 3">Option 3
<INPUT TYPE="radio" NAME="Preselection" VALUE="Option 4">Option 4
<INPUT TYPE="radio" NAME="Preselection" VALUE="Option 5">Option 5
</FORM>

On the screen, it looks like this:

Option 1
Option 2
Option 3
Option 4
Option 5

If, for some reason, you write checked at more than one INPUT tag in the same group, the browser will use the last of the choices on the list.


How do you make radio buttons do something?

When making a choice, e.g. using radio buttons, it is usually because you need the value for something. This can be done in several ways.

The most simple version is a script being executed when clicking the button. It could be something like a small alert, telling what was chosen. For this, the code looks like this:

<FORM>
<INPUT TYPE="radio" NAME="Selection" ONCLICK="alert('You have clicked Option 1')">Option 1<BR>
<INPUT TYPE="radio" NAME="Selection" ONCLICK="alert('You have clicked Option 2')">Option 2<BR>
</FORM>

On the screen, it looks like this:

Option 1
Option 2

Notice that no VALUE was specified. Here the value was put in the JavaScript instead. This is a common way of working with JavaScripts. If you have a named function, let's call it Calculation, where the value is 2, you write ONCLICK="Calculation(2)" instead of specifying VALUE="2" as an attribute and having to retrieve it.

Alerts aren't recommendable in connection with radio buttons, other than for demonstration purposes like here. Radio buttons are a fine solution, if you have sections on the page to be shown and hidden, depending on choice, e.g. a page about car sales where you click on the manufacturer, you are interested in, and a section with only models from this manufacturer will show. How this is done, will be shown in detail on another page.


If you need multiple groups of choices, this can also be done in one take. Because INPUT looks at NAME for handling of marking/not marking, you can just give your INPUT different NAMEs. It could be something like this:

<FORM>
Options, type 1<BR>
<INPUT TYPE="radio" NAME="SelectionType1" VALUE="Option 1.1">Option 1.1<BR>
<INPUT TYPE="radio" NAME="SelectionType1" VALUE="Option 1.2">Option 1.2<BR>

Options, type 2<BR>
<INPUT TYPE="radio" NAME="SelectionType2" VALUE="Option 2.1">Option 2.1<BR>
<INPUT TYPE="radio" NAME="SelectionType2" VALUE="Option 2.2">Option 2.2<BR>
</FORM>

On screen, it will look like this:

Options, type 1
Option 1.1
Option 1.2
Options, type 2
Option 2.1
Option 2.2

To style it a bit, and make it more readable, you can use some bold text, indents, etc. e.g.:

Options, type 1
Option 1.1
Option 1.2

Options, type 2
Option 2.1
Option 2.2

To have any benefit from clicking the choices, there has to be something that moved things along. That is a button, and for FORM and INPUT, this is also an INPUT, only here TYPE is "submit" instead. For this type of INPUT, VALUE is the text being written on the button. Then it looks like this:

<FORM>
Options, type 1<BR>
<INPUT TYPE="radio" NAME="SelectionType1" VALUE="Option 1.1">Option 1.1<BR>
<INPUT TYPE="radio" NAME="SelectionType1" VALUE="Option 1.2">Option 1.2<BR>

Options, type 2<BR>
<INPUT TYPE="radio" NAME="SelectionType2" VALUE="Option 2.1">Option 2.1<BR>
<INPUT TYPE="radio" NAME="SelectionType2" VALUE="Option 2.2">Option 2.2<BR>

<INPUT TYPE="submit" VALUE="Get values!">
</FORM>

Normally, for a button, you would have an ONCLICK for executing the script, but for this type of buttons, you use an ONSUBMIT in the FORM tag. Here we use a JavaScript called ShowChoices, then it looks like this:

<FORM ONSUBMIT="return ShowChoices(this)>
Options, type 1<BR>
<INPUT TYPE="radio" NAME="SelectionType1" VALUE="Option 1.1" checked>Option 1.1<BR>
<INPUT TYPE="radio" NAME="SelectionType1" VALUE="Option 1.2">Option 1.2<BR>

Options, type 2<BR>
<INPUT TYPE="radio" NAME="SelectionType2" VALUE="Option 2.1" checked>Option 2.1<BR>
<INPUT TYPE="radio" NAME="SelectionType2" VALUE="Option 2.2">Option 2.2<BR>

<INPUT TYPE="submit" VALUE="Get values!">
</FORM>

Note: To avoid having to accommodate for no choices selected, when the JavaScript starts, there is a preselection on both categories.

The JavaScript is done as an external script, i.e. as a .js file. In this case a file named GetValues.js in the directory JavaScripts. This is declared in the HEAD tag:

<SCRIPT TYPE="text/javascript" SRC="JavaScripts/GetValues.js"></SCRIPT>

The content of the JavaScript start by declaring the name of the function. By doing it this way, you can have more routines in the same file, e.g. a function named GetValue2 for getting the values from another set of radio buttons on another page. The declaration of ShowChoices looks like this:

function ShowChoices(frm) {

}

frm means that you are referring to a form. From the form, the first thing we need to do is getting the values from SelectionType1 and SelectionType2. This is done by defining two variables, radioArray1 and 2:

function ShowChoices(frm) {
var radioArray1 = frm.SelectionType1;
var radioArray2 = frm.SelectionType2;
}

We do, however, only need the values being selected, so we get a routine to parse the values from start to end, and if checked, i.e. if (radioArray1[i].checked), a variable, in this case named value1, is specified and given the INPUT's VALUE. This is done for both NAMEs. Then it looks like this:

function ShowChoices(frm) {
var radioArray1 = frm.SelectionType1;
var radioArray2 = frm.SelectionType2;

for (i=0; i < radioArray1.length; i++){
if (radioArray1[i].checked){
var value1;
value1 = radioArray1[i].value;
}
if (radioArray2[i].checked){
var value2;
value2 = radioArray2[i].value;
}
}
}

In this case, i has been set to go from 0 to radioArray1.length, i.e. the number of buttons having NAME="SelectionType1". You could also have set the value to 1, equivalent to having 2 items on the list. It is a matter of personal taste whether you want to do one or the other. By using .length you have the advantage of being able to add and remove items, without having to keep track of the length of the list. Had the two lists SelectionType1 and SelectionType2 been of different length, you would obviously use the longest list, otherwise the parsing would stop before reaching the end of the list, and you would risk not finding the selected value.

We now have the two selected values, and can use them as needed. Here we see them collected in an alert. To prevent the script from running after the alert, e.g. and go to the top of the page, you can finish off with a return false. The finished code for the JavaScript then looks like this:

function ShowChoices(frm) {
var radioArray1 = frm.SelectionType1;
var radioArray2 = frm.SelectionType2;

for (i=0; i < radioArray1.length; i++){
if (radioArray1[i].checked){
var value1;
value1 = radioArray1[i].value;
}
if (radioArray2[i].checked){
var value2;
value2 = radioArray2[i].value;
}
}
alert("You have chosen " + value1 + " and " + value2 + "!");
return false;

}

The panel with the radio buttons will look like this on the page, when it works:

Options, type 1
Option 1.1
Option 1.2

Options, type 2
Option 2.1
Option 2.2