Question
How can I make one radio button selected by default when an HTML page first loads?
For example, given this radio button:
<input type="radio" name="imgsel" value="">
What attribute should be added so that it starts in the selected state?
Short Answer
You will learn how HTML radio groups work and how to choose their initial selection with the checked attribute. You will also see why radio buttons need a shared name, how browsers handle multiple defaults, and when JavaScript should update a selection instead.
Concept
Radio buttons represent a choice from a mutually exclusive group: a user can select one option from that group.
To make an option selected when the page first loads, add the Boolean HTML attribute checked to that radio input:
<input type="radio" name="imgsel" value="small" checked>
The shared name attribute defines the group. Radio buttons with the same name belong together, so selecting one unselects the others.
The checked attribute describes the control's initial, default state in the HTML document. It is useful when an application has a sensible default choice, such as a default delivery method or display size.
For accessible forms, each radio button should also have a visible text label associated with it using label and id.
Mental Model
Think of a radio group as a row of preset buttons on a car radio. Every button belongs to the same station selector, but only one can be pressed in at a time.
nameis the label on the selector that says which buttons belong together.valueis the information sent when a particular button is chosen.checkedis the button that starts pressed when the page opens.
If two buttons in the same group are marked checked in the HTML, the browser resolves the conflict and only one ends up selected. Do not rely on that behavior; explicitly mark only the intended default.
Syntax and Examples
Use checked directly on the radio input that should start selected.
<form>
<label>
<input type="radio" name="imgsel" value="small" checked>
Small image
</label>
<label>
<input type="radio" name="imgsel" value="medium">
Medium image
</label>
<label>
<input type="radio" name="imgsel" value="large">
Large image
</label>
</form>
When this page loads, Small image is selected.
Important attributes
| Attribute | Purpose |
|---|---|
type="radio" | Creates a radio button. |
name="imgsel" | Places related radio buttons in the same group. |
value="small" | The value submitted if this option is selected. |
checked | Makes this option selected initially. |
id and label for | Connect visible label text to the input. |
A version with explicit labels is often easier to maintain:
<input id="size-small" type="radio" name="imgsel" value="small" checked>
<label for="size-small">Small image</label>
<input id="size-large" type="radio" name="imgsel" value="large">
<label for="size-large">Large image</label>
Clicking either label selects its associated radio button.
Step by Step Execution
Consider this form:
<form action="/preview" method="get">
<label><input type="radio" name="theme" value="light" checked> Light</label>
<label><input type="radio" name="theme" value="dark"> Dark</label>
<button type="submit">Save</button>
</form>
- The browser reads the first input and creates a radio button in the
themegroup. - It sees
checked, so thelightradio button is initially selected. - The browser reads the second input. Its
nameis alsotheme, so it joins the same group. - If the user selects Dark, the browser automatically clears the selection from Light.
- If the user submits without making a change, the form sends
theme=lightin the request. - If the user selects Dark before submitting, the form sends
theme=darkinstead.
Only the currently selected radio button contributes its name=value pair during form submission.
Real World Use Cases
Radio buttons work well when users must choose exactly one option from a small, known set.
- Shipping forms: Start with “Standard delivery” selected.
- Settings pages: Default a theme to “Use system setting.”
- Image tools: Preselect a common output size such as “Medium.”
- Checkout flows: Select a preferred payment method when appropriate.
- Search filters: Choose “Most relevant” as the initial sorting option.
- Setup wizards: Provide a recommended configuration while still allowing users to change it.
Choose a default only when it is reasonable. For a sensitive decision, such as accepting optional marketing messages, it is often better to require an explicit user choice.
Real Codebase Usage
In real projects, the initial selection may come from server-rendered data, a saved user setting, or application state.
Server-rendered conditional default
A template engine commonly adds checked only when a stored value matches an option:
<input
type="radio"
name="plan"
value="pro"
checked
>
The exact conditional syntax depends on the server framework, but the rendered HTML should contain checked on only one radio in a group.
Updating selection with JavaScript
Use the DOM property when the default must change after the page has loaded:
<input id="monthly" type="radio" name="billing" value="monthly" checked>
<input id="yearly" type="radio" name="billing" value="yearly">
<script>
document.querySelector('#yearly').checked = true;
</script>
Setting .checked = true selects #yearly and clears the other radio buttons in the same named group.
Validation pattern
For a required decision, put required on one radio button in the group:
<label><input type="radio" name="contact" value="email" required> Email</label>
<label><input type="radio" name="contact" value="phone"> Phone</label>
If no default is selected, the browser prevents submission until the user chooses one. If a radio is already checked, the group already satisfies required.
Common Mistakes
Giving each radio button a different name
This creates separate groups, allowing multiple options to be selected:
<!-- Incorrect for one-choice behavior -->
<input type="radio" name="small" value="small">
<input type="radio" name="large" value="large">
Use the same name for all choices in one group:
<input type="radio" name="size" value="small">
<input type="radio" name="size" value="large">
Using selected instead of checked
selected belongs to an <option> inside a <select>, not to radio buttons:
<!-- Incorrect -->
<input type="radio" name="size" value="small" selected>
Use checked:
<input type="radio" name="size" value="small" checked>
Marking multiple options as checked
<!-- Avoid this -->
<input type="radio" name="size" value="small" checked>
<input type="radio" name="size" value="large" checked>
A group should have zero or one intended default. Mark only one option with checked.
Omitting useful value attributes
A selected radio without a meaningful value gives the server less useful data.
<!-- Less useful -->
<input type="radio" name="imgsel" value="" checked>
Use a meaningful value:
<input type="radio" name="imgsel" value="medium" checked>
Changing only the HTML attribute with JavaScript
For a live page, use the property:
radio.checked = true;
This updates the current selection. The checked HTML attribute primarily represents the initial default state.
Comparisons
| Control or technique | Best use | Default-state attribute/property |
|---|---|---|
| Radio buttons | Choose one option from a small set | HTML: checked; JavaScript: .checked |
| Checkboxes | Choose zero, one, or many independent options | HTML: checked; JavaScript: .checked |
<select> dropdown | Choose one option while saving space | Put selected on an <option> |
| Text input | Enter free-form text | HTML: value="..."; JavaScript: .value |
Radio buttons versus checkboxes
Use radio buttons when options are mutually exclusive:
<label><input type="radio" name="format" value="pdf" checked> PDF</label>
<label><input type="radio" name="format" value="csv"> CSV</label>
Use checkboxes when choices are independent:
<label><input type="checkbox" name="features" value="email"> Email alerts</label>
<label><input type="checkbox" name="features" value="sms"> SMS alerts</label>
Both checkboxes can be selected, while only one format radio button can be selected.
Cheat Sheet
<!-- One radio group with a default choice -->
<label>
<input type="radio" name="groupName" value="optionA" checked>
Option A
</label>
<label>
<input type="radio" name="groupName" value="optionB">
Option B
</label>
- Use
checkedto select a radio button by default. - Give every radio in one choice set the same
name. - Give each option a distinct, meaningful
value. - Add
checkedto only one intended default per group. - Use
selectedonly for<option>elements in<select>. - In JavaScript, change a live selection with
element.checked = true. - Associate text with controls using a wrapping
<label>or matchinglabel forand inputid. - Selected radio buttons submit only the selected option's
name=valuepair.
FAQ
How do I set a radio button as checked by default in HTML?
Add the checked attribute to the input:
<input type="radio" name="color" value="blue" checked>
Can more than one radio button be checked at once?
Not when they have the same name. A radio group is designed to have one active choice. Mark only one option as the intended default.
Why can I select multiple radio buttons on my page?
They probably have different name values. Use the same name for options that belong to one group.
Is checked="checked" valid?
Yes. checked is a Boolean attribute, so modern HTML usually uses the shorter form: checked.
Should I use checked or selected for radio buttons?
Use checked. The selected attribute is for <option> elements in a <select> control.
How do I change the selected radio button with JavaScript?
Set the input's .checked property:
document.querySelector('#dark-theme').checked = true;
Does a default radio button get sent with a form?
Yes. If it remains selected at submission, its name and value are submitted. For example, name="size" value="medium" sends size=medium.
Mini Project
Description
Build a small image-size preference form. It demonstrates a practical radio group with a default choice, clear labels, meaningful submitted values, and a preview message that updates when the user changes the selection.
Goal
Create a form where “Medium” is selected by default and the chosen image size is shown after submission.
Requirements
- Create three radio buttons for Small, Medium, and Large image sizes.
- Place all three radio buttons in the same group.
- Make Medium the default selection.
- Give each option a meaningful value.
- Add an accessible label for every radio button.
- Display the selected size when the form is submitted.
Keep learning
Related questions
Add a Favicon to a Static HTML Page
Learn how to add a favicon to static HTML pages, use correct paths and icon formats, and troubleshoot browser caching and local-file issues.
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
Apply CSS !important with jQuery .css()
Learn why jQuery .css() cannot set !important and how to use CSSStyleDeclaration.setProperty() to apply inline important styles safely.