π MODULE 2 β Forms & User Input
π Lesson 2.3 β Radio Buttons & Checkboxes
π Allowing Users to Select One or Multiple Options in Forms
π― Learning Objectives
After completing this lesson, students will be able to:
β Understand Radio Buttons
β Understand Checkboxes
β Know the difference between Radio Buttons and Checkboxes
β Create Single Choice Questions
β Create Multiple Choice Questions
β Build Professional Forms
β Create Admission and Registration Forms
π Introduction
Imagine a college admission form.
Question:
Gender
- Male
- Female
- Other
Student can select:
β Only One Option
Another question:
Skills
- HTML
- CSS
- JavaScript
- Python
Student can select:
β One Skill
β Two Skills
β Three Skills
β All Skills
To solve these situations HTML provides:
π Radio Buttons
and
βοΈ Checkboxes
π€ What is a Radio Button?
Radio Buttons allow users to select:
Only One Option
from multiple choices.
Real Life Examples
Gender Selection
( ) Male
( ) Female
( ) Other
Only one can be selected.
Payment Method
( ) Cash
( ) UPI
( ) Debit Card
Only one option.
π Creating Radio Buttons
Syntax:
<input type="radio">
Example
<label>Male</label>
<input type="radio">
Output
β Male
Multiple Radio Buttons
<input type="radio"> Male
<br>
<input type="radio"> Female
<br>
<input type="radio"> Other
Problem
All buttons work independently.
Multiple selections become possible.
β Wrong
π― Name Attribute
Radio buttons must share the same name.
Example:
<input type="radio" name="gender">
Correct Example
<input type="radio" name="gender">
Male
<br>
<input type="radio" name="gender">
Female
<br>
<input type="radio" name="gender">
Other
Output
β Male
β Female
β Other
Now only one option can be selected.
β Correct
π Value Attribute
Stores selected data.
Example:
<input
type="radio"
name="gender"
value="Male">
Example
<input
type="radio"
name="gender"
value="Male">
Male
Why Value is Important?
When the form is submitted:
Male
is stored.
π― Checked Attribute
Used to select a default option.
Example:
<input
type="radio"
name="gender"
checked>
Result
Option selected automatically.
Example
<input
type="radio"
name="gender"
checked>
Male
<br>
<input
type="radio"
name="gender">
Female
Output
Male selected by default.
βοΈ What is a Checkbox?
Checkboxes allow users to select:
Multiple Options
Real Life Example
Skills
β HTML
β CSS
β JavaScript
β Python
Students can select:
β HTML
β CSS
β JavaScript
All together.
Creating Checkboxes
Syntax:
<input type="checkbox">
Example
<input type="checkbox">
HTML
Output
β HTML
Multiple Checkboxes
<input type="checkbox">
HTML
<br>
<input type="checkbox">
CSS
<br>
<input type="checkbox">
JavaScript
Output
β HTML
β CSS
β JavaScript
π Value Attribute in Checkboxes
Example:
<input
type="checkbox"
value="HTML">
Example
<input
type="checkbox"
value="HTML">
HTML
Result
When selected:
HTML
is stored.
π― Checked Checkbox
Default selected checkbox.
Example:
<input
type="checkbox"
checked>
Output
β HTML
Already selected.
π Radio vs Checkbox
| Radio Button | Checkbox |
|---|---|
| Single Choice | Multiple Choices |
| One Option | Many Options |
| Circular Shape | Square Shape |
| Gender Selection | Skills Selection |
| Payment Method | Hobbies |
Example β Gender Selection
<h3>Gender</h3>
<input
type="radio"
name="gender">
Male
<br>
<input
type="radio"
name="gender">
Female
<br>
<input
type="radio"
name="gender">
Other
Output
β Male
β Female
β Other
Only one option.
Example β Skills Selection
<h3>Skills</h3>
<input
type="checkbox">
HTML
<br>
<input
type="checkbox">
CSS
<br>
<input
type="checkbox">
JavaScript
<br>
<input
type="checkbox">
Python
Output
β HTML
β CSS
β JavaScript
β Python
Multiple selections allowed.
π― Complete Example Program
<!DOCTYPE html>
<html>
<head>
<title>Radio and Checkbox Example</title>
</head>
<body>
<h1>Student Registration Form</h1>
<form>
<h3>Gender</h3>
<input
type="radio"
name="gender">
Male
<br>
<input
type="radio"
name="gender">
Female
<br>
<input
type="radio"
name="gender">
Other
<br><br>
<h3>Skills</h3>
<input
type="checkbox">
HTML
<br>
<input
type="checkbox">
CSS
<br>
<input
type="checkbox">
JavaScript
<br>
<input
type="checkbox">
Python
</form>
</body>
</html>
π― Expected Output
Student Registration Form
Gender
β Male
β Female
β Other
Skills
β HTML
β CSS
β JavaScript
β Python
π« Real World Example β College Admission Form
<h3>Gender</h3>
<input
type="radio"
name="gender">
Male
<br>
<input
type="radio"
name="gender">
Female
<br>
<input
type="radio"
name="gender">
Other
<br><br>
<h3>Interests</h3>
<input
type="checkbox">
Sports
<br>
<input
type="checkbox">
Music
<br>
<input
type="checkbox">
Programming
π Using Labels with Radio Buttons
Professional method:
<label>
<input
type="radio"
name="gender">
Male
</label>
Advantage
Clicking on text also selects the option.
β οΈ Common Beginner Mistakes
Mistake 1
Different Names for Radio Buttons
Wrong:
<input type="radio" name="a">
<input type="radio" name="b">
Both can be selected.
β Wrong
Correct:
<input type="radio" name="gender">
<input type="radio" name="gender">
Mistake 2
Using Radio Instead of Checkbox
Wrong:
Skills:
<input type="radio">
Only one skill can be selected.
Correct:
<input type="checkbox">
Mistake 3
Forgetting Value Attribute
Wrong:
<input type="radio">
Better:
<input
type="radio"
value="Male">
π Best Practices
β Use Radio Buttons for single choice.
β Use Checkboxes for multiple choices.
β Always use labels.
β Use value attributes.
β Use meaningful names.
π Mini Project β Student Interest Form
Create:
Gender
β Male
β Female
β Other
Skills
β HTML
β CSS
β JavaScript
β Python
Hobbies
β Reading
β Gaming
β Music
β Sports
π Practical Activity
Create a form containing:
Gender
Radio Buttons
Branch
Radio Buttons
Skills
Checkboxes
Hobbies
Checkboxes
Run it in Chrome Browser.
π Assignment
Assignment 2.3
- What is a Radio Button?
- What is a Checkbox?
- What is the difference between them?
- Why is the name attribute important?
- What is the purpose of the value attribute?
- Create a gender selection form.
- Create a skills selection form.
π§ Quiz
Q1. Which input type creates radio buttons?
A. check
B. select
C. radio
D. option
β Answer: C
Q2. Radio buttons are used for?
A. Multiple choices
B. Single choice
C. Images
D. Tables
β Answer: B
Q3. Which input type creates checkboxes?
A. radio
B. select
C. checkbox
D. option
β Answer: C
Q4. Checkboxes allow?
A. One selection
B. Multiple selections
C. No selection
D. Images
β Answer: B
Q5. Which attribute groups radio buttons together?
A. value
B. id
C. name
D. checked
β Answer: C
π Lesson Summary
β Radio Buttons allow one option to be selected.
β Checkboxes allow multiple options to be selected.
β
Radio buttons use the same name attribute.
β
The value attribute stores data.
β
The checked attribute selects options by default.
β Radio buttons and checkboxes are widely used in registration and admission forms.