Checkboxes Radio Buttons in HTML

4/6/2025

Indroduction of Checkboxes and Radio Buttons in HTML

Go Back

Working with Checkboxes and Radio Buttons in HTML: A Beginner’s Guide

Introduction

When building web forms, checkboxes and radio buttons are essential for collecting user input. These form controls allow users to make selections, whether it's multiple options or just one. In this guide, we’ll walk you through how to work with checkboxes and radio buttons in HTML — from basic syntax to practical use cases.

Whether you’re a beginner or brushing up on your HTML skills, this article will help you confidently use these input types in your web forms.


 Indroduction of Checkboxes and Radio Buttons in HTML

What Are Checkboxes and Radio Buttons?

✅ Checkboxes

Checkboxes allow users to select multiple options from a list. Each checkbox works independently, so users can check one, multiple, or no options.

Example:

<form>
  <label><input type="checkbox" name="skills" value="HTML"> HTML</label><br>
  <label><input type="checkbox" name="skills" value="CSS"> CSS</label><br>
  <label><input type="checkbox" name="skills" value="JavaScript"> JavaScript</label>
</form>

🔘 Radio Buttons

Radio buttons let users select only one option from a group. All radio buttons in a group share the same name attribute.

Example:

<form>
  <label><input type="radio" name="gender" value="male"> Male</label><br>
  <label><input type="radio" name="gender" value="female"> Female</label><br>
  <label><input type="radio" name="gender" value="other"> Other</label>
</form>

Key Differences Between Checkboxes and Radio Buttons

Feature Checkboxes Radio Buttons
Selection type Multiple Single
Input type type="checkbox" type="radio"
Use case example Skills, Interests Gender, Age Group

Grouping and Labeling: Best Practices

✅ Use <label> for better accessibility

Associating inputs with labels helps users (and screen readers) understand the form.

<label for="subscribe">Subscribe</label>
<input type="checkbox" id="subscribe" name="newsletter">

✅ Use the same name for radio groups

This ensures only one option can be selected at a time.

<input type="radio" name="color" value="red">
<input type="radio" name="color" value="blue">
<input type="radio" name="color" value="green">

How to Get Selected Values with JavaScript

Here’s how to handle checkbox and radio button values using JavaScript.

✔️ Get checked checkboxes

const checkboxes = document.querySelectorAll('input[name="skills"]:checked');
let values = [];
checkboxes.forEach((checkbox) => {
  values.push(checkbox.value);
});
console.log(values);

✔️ Get selected radio button

const selected = document.querySelector('input[name="gender"]:checked');
console.log(selected.value);

Real-World Use Cases

  • ✅ Checkboxes: Selecting multiple interests, newsletter topics, or product features.

  • 🔘 Radio Buttons: Choosing payment methods, survey options, or delivery preferences.


Common Mistakes to Avoid

  • Giving each radio button a different name — it breaks the group behavior.

  • Missing value attributes — your backend won't receive proper data.

  • Forgetting labels — hurts accessibility and UX.


Final Thoughts

Understanding how to work with checkboxes and radio buttons is vital for building intuitive and accessible forms. These input types may seem simple, but when used correctly, they significantly enhance user interaction and data collection.

Whether you’re creating a simple contact form or a complex survey, mastering these elements will help you build better, user-friendly websites.


Want more HTML tips and tutorials? Explore our full collection on developerIndian for beginner-friendly web development guides.

Table of content