Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to HTML Notes
Topic #713

Radio Button

<input type="radio"> lets a user pick exactly one option from a group — sharing the same name is what turns several radio buttons into one mutually exclusive group.

Syntax

<input type="radio" name="plan" value="basic" id="plan-basic">
<label for="plan-basic">Basic</label>

<input type="radio" name="plan" value="pro" id="plan-pro">
<label for="plan-pro">Pro</label>

Basic Example

<fieldset>
    <legend>Preferred class mode</legend>
    <input type="radio" id="mode-live" name="mode" value="live" checked>
    <label for="mode-live">Live Online</label>

    <input type="radio" id="mode-recorded" name="mode" value="recorded">
    <label for="mode-recorded">Recorded</label>
</fieldset>

Browser output: two circular buttons; selecting one automatically deselects the other, because they share the same name="mode". Only ever one can be checked within that shared group at a time.

How It Works

Only the currently selected radio button in a named group submits its value — every unselected one in that group sends nothing, exactly like an unchecked checkbox. If none is pre-checked and the user never interacts with the group, no value is submitted at all for that field.

Important Attributes

AttributePurpose
nameMust be identical across every button in the same mutually-exclusive group
valueWhat's submitted for that specific option if it's the one selected
checkedPre-selects one option as the default

Comparison: checkbox vs radio

checkboxradio
Selections allowedAny number, including zero or allExactly one within a shared name group
Typical use"Select all that apply""Choose exactly one option"

Real-World Example

<fieldset>
    <legend>Payment Method</legend>
    <input type="radio" id="pay-card" name="payment" value="card" checked>
    <label for="pay-card">Credit/Debit Card</label>

    <input type="radio" id="pay-upi" name="payment" value="upi">
    <label for="pay-upi">UPI</label>
</fieldset>

A textbook radio-button use case — payment method selection where exactly one option must be chosen, never more than one.

Common Mistakes

  • Giving each radio button in what should be one group a different name — this accidentally breaks the mutual exclusivity, letting multiple "options" be selected at once
  • Leaving no option pre-checked when one really should be the sensible default

Best Practices

  • Always use the exact same name across every button in a group
  • Group radio buttons inside a <fieldset>/<legend> describing the overall choice being made

Accessibility Considerations

Screen readers announce a radio group's total option count and current position ("radio button, 2 of 3") when the group is correctly built with a shared name and a <legend> describing the choice.

SEO Considerations

No direct ranking effect.

Browser & Modern HTML Notes

Arrow keys move focus between radio buttons that share the same name, automatically selecting each one as you go — genuinely useful, native keyboard behavior with zero extra code.

Mini Practice

  1. Build a 3-option radio group for choosing a class time slot, with one pre-selected.
  2. Spot and fix a radio group where each button was accidentally given a different name.

Related HTML Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →