Section 20: Radio Buttons, Checkboxes and Button Types
Learning Objectives
- Understand the difference between radio buttons and checkboxes.
- Group radio buttons correctly.
- Understand submit, reset and button types.
- Use buttons safely inside and outside forms.
1. Radio Buttons
Radio buttons are appropriate when users should choose one option from a group.
<fieldset>
<legend>Choose a payment method</legend>
<label>
<input type="radio" name="payment" value="upi" required>
UPI
</label>
<label>
<input type="radio" name="payment" value="card">
Credit or Debit Card
</label>
<label>
<input type="radio" name="payment" value="cash">
Cash
</label>
</fieldset>
Radio buttons with the same name form a group. If the user selects one option, the other options in that group become unselected.
2. Checkboxes
Checkboxes are appropriate for independent choices, such as selecting multiple interests or accepting a required agreement.
<fieldset>
<legend>Select your skills</legend>
<label>
<input type="checkbox" name="skills[]" value="html">
HTML
</label>
<label>
<input type="checkbox" name="skills[]" value="css">
CSS
</label>
<label>
<input type="checkbox" name="skills[]" value="php">
PHP
</label>
</fieldset>
Several checkboxes can be checked at once. Using skills[] is a common convention for submitting multiple values to server frameworks, but HTML itself does not require the brackets.
3. Required Agreement Checkbox
<label>
<input type="checkbox" name="terms" value="accepted" required>
I agree to the terms and conditions.
</label>
With required, the checkbox must be checked before normal browser form validation allows submission. The server must still verify the submitted agreement where necessary.
4. Three Main Button Types
- submit: submits the form.
- reset: restores controls to their initial values.
- button: has no default form-submission behavior.
<form action="/register" method="post">
<label for="username">Username</label>
<input id="username" name="username" required>
<button type="submit">Register</button>
<button type="reset">Clear Form</button>
<button type="button">Check Availability</button>
</form>
A button with type="button" requires JavaScript or another application behavior to perform an action. A reset button does not necessarily clear every field to an empty value; it restores each control's initial state.
5. Why Explicit Button Types Matter
A button associated with a form can default to submit when its type is omitted. Always set the type explicitly to prevent accidental submissions.
<button type="button">Open Help</button>
<button type="submit">Save</button>
6. Buttons Outside Forms
<button type="button">Open Menu</button>
Outside a form, an explicit button type still communicates its intended behavior. Use JavaScript to make it open a menu, dialog or another interface feature.
Common Mistakes
- Giving related radio buttons different names.
- Using radio buttons when multiple selections are needed.
- Using checkboxes when only one option should be selectable.
- Forgetting type="button" for a button that should not submit a form.
- Assuming that client-side required validation replaces server-side validation.
Practice Exercises
- Create radio buttons for selecting a gender-neutral title such as Mr, Ms or Mx.
- Create checkboxes for selecting multiple programming skills.
- Create a required terms-and-conditions checkbox.
- Create submit, reset and ordinary buttons in one form.
- Explain why related radio buttons must share the same name.
Revision Quiz
- Which control is used to choose one option from a group?
- Which control supports several independent selections?
- Which attribute groups radio buttons together?
- Which button type submits a form?
- Which button type has no default form-submission behavior?
- What does a reset button do?
Answer Key and Explanations
- Radio button — It is used for a single choice within a group.
- Checkbox — Multiple independent checkboxes can be checked.
- name — Matching names create a radio group.
- submit — It initiates form submission.
- button — It requires application behavior for an action.
- Restores initial values — It does not necessarily empty every field.