🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEhtml

html Documentation

LOADING ENGINE...

Checkboxes

AI & DATA SCIENCE // checkboxes

Checkboxes (<input type="checkbox">) let a user toggle an independent true/false (or, in groups, multiple-selection) option.

Syntax

<input type="checkbox" name="subscribe" checked> Subscribe to newsletter

Deep Dive Course

Unlike radio buttons, checkboxes are **independent** of each other — checking one doesn't affect any others, even if they share the same `name`. Multiple checkboxes sharing a name (often written as `name="toppings[]"` on the server side) let a user select any number of options from a list, and the form submits a value for each one that's checked. A checkbox's `checked` state is what determines whether it's included in the submission at all — an unchecked box submits nothing for that name.

1Understanding Checkboxes

Unlike radio buttons, checkboxes are independent of each other — checking one doesn't affect any others, even if they share the same name. Multiple checkboxes sharing a name (often written as name="toppings[]" on the server side) let a user select any number of options from a list, and the form submits a value for each one that's checked. A checkbox's checked state is what determines whether it's included in the submission at all — an unchecked box submits nothing for that name.

💡

An unchecked checkbox contributes NOTHING to the submitted form data — not even an empty value — so server-side code must treat 'field absent' as equivalent to false/unchecked, not throw an error.

editor.html
<label><input type="checkbox" name="terms" required> I agree to the terms</label>
localhost:3000

2Practical Example

Here is a real-world application of Checkboxes showing how it is used in production HTML.

editor.html
<!-- Multiple independent checkboxes for a 'select any' list -->
<label><input type="checkbox" name="toppings" value="cheese"> Cheese</label>
<label><input type="checkbox" name="toppings" value="pepperoni" checked> Pepperoni</label>
localhost:3000

3Best Practices

Follow these guidelines when working with Checkboxes:

1. Use checkboxes for independent yes/no toggles, or for 'select any number' from a list

2. Use the checked attribute to set a sensible default state

3. Remember unchecked boxes are omitted entirely from submitted data — don't assume they'll arrive as false

⚠️

Tip: An unchecked checkbox contributes NOTHING to the submitted form data — not even an empty value — so server-side code must treat 'field absent' as equivalent to false/unchecked, not throw an error.

editor.html
<label><input type="checkbox" name="terms" required> I agree to the terms</label>
localhost:3000

Examples

Example 01Basic Usage
<label><input type="checkbox" name="terms" required> I agree to the terms</label>
Example 02Advanced Example
<!-- Multiple independent checkboxes for a 'select any' list -->
<label><input type="checkbox" name="toppings" value="cheese"> Cheese</label>
<label><input type="checkbox" name="toppings" value="pepperoni" checked> Pepperoni</label>

Best Practices

  • Use checkboxes for independent yes/no toggles, or for 'select any number' from a list
  • Use the checked attribute to set a sensible default state
  • Remember unchecked boxes are omitted entirely from submitted data — don't assume they'll arrive as false

Interview Question

If a form has an unchecked checkbox, what value (if any) gets submitted for it?

Hint: Think about how this differs from a text input, which always submits something (even an empty string).

An unchecked checkbox submits absolutely nothing — its name/value pair is entirely omitted from the form data, unlike a text input which always submits at least an empty string. This means server-side or JavaScript form-handling code has to check for the field's ABSENCE to correctly treat it as 'unchecked/false', rather than expecting an explicit false value to arrive.

Exercises

EasyPractice using Checkboxes in a real scenario.
View Solution
<label><input type="checkbox" name="terms" required> I agree to the terms</label>

Frequently Asked Questions

If a form has an unchecked checkbox, what value (if any) gets submitted for it?

An unchecked checkbox submits absolutely nothing — its name/value pair is entirely omitted from the form data, unlike a text input which always submits at least an empty string. This means server-side or JavaScript form-handling code has to check for the field's ABSENCE to correctly treat it as 'unchecked/false', rather than expecting an explicit false value to arrive.

Related Functions

input-tagRadio Buttons