🚀 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...

Radio Buttons

AI & DATA SCIENCE // radio-buttons

Radio buttons (<input type="radio">) let a user pick exactly one option from a set — sharing the same name attribute is what links them into a single mutually-exclusive group.

Syntax

<input type="radio" name="plan" value="free" checked> Free
<input type="radio" name="plan" value="pro"> Pro

Deep Dive Course

A group of radio buttons is created simply by giving multiple `<input type="radio">` elements the **same `name`** — the browser then enforces that only one of them can be checked at a time, automatically unchecking any previously selected option in that group when a new one is picked. Only the CHECKED radio button's `value` gets submitted with the form under that shared name; unchecked ones contribute nothing.

1Understanding Radio Buttons

A group of radio buttons is created simply by giving multiple <input type="radio"> elements the same `name` — the browser then enforces that only one of them can be checked at a time, automatically unchecking any previously selected option in that group when a new one is picked. Only the CHECKED radio button's value gets submitted with the form under that shared name; unchecked ones contribute nothing.

💡

Forgetting to give every radio button in a group the exact same name is one of the most common form bugs — without a shared name, each one behaves as its own independent (and pointless) single-option group.

editor.html
<fieldset>
  <legend>Preferred size</legend>
  <label><input type="radio" name="size" value="s"> Small</label>
  <label><input type="radio" name="size" value="m" checked> Medium</label>
  <label><input type="radio" name="size" value="l"> Large</label>
</fieldset>
localhost:3000

2Practical Example

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

editor.html
<!-- Reading the selected radio value in JavaScript -->
<script>
  const selected = document.querySelector('input[name="size"]:checked');
  console.log(selected.value); // 'm'
</script>
localhost:3000

3Best Practices

Follow these guidelines when working with Radio Buttons:

1. Give every radio button in the same logical group the identical name attribute

2. Wrap the whole group in a <fieldset> with a <legend> describing the overall question

3. Pre-check a sensible default with the checked attribute when one option should be selected initially

⚠️

Tip: Forgetting to give every radio button in a group the exact same name is one of the most common form bugs — without a shared name, each one behaves as its own independent (and pointless) single-option group.

editor.html
<fieldset>
  <legend>Preferred size</legend>
  <label><input type="radio" name="size" value="s"> Small</label>
  <label><input type="radio" name="size" value="m" checked> Medium</label>
  <label><input type="radio" name="size" value="l"> Large</label>
</fieldset>
localhost:3000

Examples

Example 01Basic Usage
<fieldset>
  <legend>Preferred size</legend>
  <label><input type="radio" name="size" value="s"> Small</label>
  <label><input type="radio" name="size" value="m" checked> Medium</label>
  <label><input type="radio" name="size" value="l"> Large</label>
</fieldset>
Example 02Advanced Example
<!-- Reading the selected radio value in JavaScript -->
<script>
  const selected = document.querySelector('input[name="size"]:checked');
  console.log(selected.value); // 'm'
</script>

Best Practices

  • Give every radio button in the same logical group the identical name attribute
  • Wrap the whole group in a
    with a describing the overall question
  • Pre-check a sensible default with the checked attribute when one option should be selected initially

Interview Question

What actually links several <input type="radio"> elements into one mutually-exclusive group?

Hint: It's not their position in the DOM, and not being inside the same <fieldset>.

Purely the shared name attribute — the browser groups all radio inputs with the identical name value into one mutually-exclusive set, regardless of where they physically sit in the document (they don't even need to be adjacent or in the same fieldset, though that's good practice for clarity). Two radio inputs with different name values are entirely independent, each acting as its own separate one-option 'group', which is a very common source of bugs when a typo breaks the shared name.

Exercises

EasyPractice using Radio Buttons in a real scenario.
View Solution
<fieldset>
  <legend>Preferred size</legend>
  <label><input type="radio" name="size" value="s"> Small</label>
  <label><input type="radio" name="size" value="m" checked> Medium</label>
  <label><input type="radio" name="size" value="l"> Large</label>
</fieldset>

Frequently Asked Questions

What actually links several <input type="radio"> elements into one mutually-exclusive group?

Purely the shared name attribute — the browser groups all radio inputs with the identical name value into one mutually-exclusive set, regardless of where they physically sit in the document (they don't even need to be adjacent or in the same fieldset, though that's good practice for clarity). Two radio inputs with different name values are entirely independent, each acting as its own separate one-option 'group', which is a very common source of bugs when a typo breaks the shared name.

Related Functions

input-tagCheckboxesfieldset-tag