spacer

Checkboxes & Radio Buttons

 
spacer
Interactive web forms wouldn't be much fun (or convenient) if all they did was provide a series of text fields for the user to type in their information. Quite often the input that you need would be better obtained by simply supplying a list of choices. The user could then quickly scan through the list and select an applicable item. You can create such lists by using either groups of checkboxes or groups of radio buttons.


Checkboxes


You can prompt users for specific input by using a list of checkboxes. None, one or more checkboxes may be checked by the user according to the information they wish to send with the form. A checkbox is created by using the input element with the type="checkbox" attribute-value pair.

Here is a list of checkboxes prompting the user to select one or more favourite colors.

My favourite colors are:

Red
Yellow
Blue
Orange
Green
Purple
SOURCE CODE

My favourite colors are:<br><br>

<input type="checkbox" name="color" value="red">
Red
<br>

<input type="checkbox" name="color" value="yellow">
Yellow
<br>

<input type="checkbox" name="color" value="blue">
Blue
<br>

<input type="checkbox" name="color" value="orange">
Orange
<br>

<input type="checkbox" name="color" value="green">
Green
<br>

<input type="checkbox" name="color" value="purple">
Purple
<br>


HOW CHECKBOXES WORK

A group of checkboxes is created by giving two or more checkboxes the same control name. In the above example, the checkbox group control name is "color". However each checkbox in the group has a different associated value specified by the value attribute. When a user selects (checks) a checkbox, its value gets assigned as the current value of the checkbox group's control name. A checkbox group's control name may conceivably get paired with several current values if the user selects more than one checkbox.



Radio Buttons


Radio buttons work just like checkboxes except they are typically set up to be mutually exclusive of one another, i.e. when one is selected, all the others are automatically 'deselected'. A radio button is created by using the input element with the type="radio" attribute-value pair.

Here's a group of radio buttons prompting the user to select his current web browser.
Your current web browser is:

Internet Explorer
Mozilla
Opera
SOURCE CODE

Your current web browser is:<br><br>

<input type="radio" name="browser" value="IE" checked>
Internet Explorer<br>

<input type="radio" name="browser" value="Mozilla">
Mozilla<br>

<input type="radio" name="browser" value="Opera">
Opera<br>


HOW RADIO BUTTONS WORK

A group of radio buttons is created by giving two or more radio buttons the same control name. In the above example, the radio button group control name is "browser". However each radio button in the group has a different associated value specified by the value attribute. When a user selects a radio button, its value gets assigned as the current value of the radio button group's control name. Since a selected radio button 'deselects' all other radio buttons within the same group then a radio button group's control name only gets paired with one current value at any one time.


DEFAULT SELECTION

Each radio button group should have a radio button selected by default so that it is implied to the user that only one radio button may be selected at any one time. This is accomplished by inserting the checked attribute. In the above example, the Internet Explorer radio button carries the checked attribute.


Now let's suppose you want to create a list of items for the user to select from but your list is either extensive and/or you don't want to use up alot of space to display it. Well then what you're looking for here is a dropdown menu...