References

Beginner-friendly references for web development, with live, editable examples.

The HTML checked attribute

Attribute All modern browsers
In short

A checkbox or radio button with the HTML checked attribute starts out selected, as in <input type="checkbox" checked>. The attribute sets the default state. Clicks change the checked property instead, and a form reset brings back what the attribute says.

Overview

The HTML checked attribute marks a checkbox or radio button as selected when the page loads. Checkboxes and radio buttons are the two <input> types that carry it. The attribute needs no value, since its presence alone turns the control on.

The attribute holds the default, and the checked property holds the current state. When someone unticks a box that has the attribute, the property turns false but the attribute stays, so outerHTML still shows it. The defaultChecked property reflects the attribute.

Once the user toggles a box, or a script sets its checked property, the attribute only changes the default. Adding or removing it then leaves the tick alone. A form reset puts each box back to what its attribute says and lets the attribute control the state again.

Radio buttons with the same name make up a group when they also share a form, or when none of them has one. Only one button in a group can be checked, and checking one, by a click or from a script, unchecks the others.

Syntax

<label>
  <input type="checkbox" name="newsletter" value="weekly" checked>
  Send me the weekly newsletter
</label>

Values

Value
A boolean attribute. If present, the checkbox or radio button starts out checked.

Example

Live example
<form id="prefs">
  <label><input type="checkbox" id="news" name="news" value="weekly" checked> Weekly newsletter</label><br>
  <label><input type="checkbox" name="offers" value="yes"> Partner offers</label><br>
  <label><input type="radio" name="format" value="html" checked> HTML email</label>
  <label><input type="radio" name="format" value="text"> Plain text</label><br>
  <button type="reset">Reset</button>
  <p id="msg" role="status"></p>
</form>
<p>Untick Weekly newsletter, then press Reset.</p>
<script>
const form = document.getElementById('prefs');
const box = document.getElementById('news');
const msg = document.getElementById('msg');
function show() {
  const sent = [...new FormData(form)].map(e => e.join('=')).join('&');
  msg.textContent = 'Newsletter attribute ' + (box.hasAttribute('checked') ? 'present' : 'absent') +
    ', property ' + box.checked + '. Form sends ' + (sent || 'nothing') + '.';
}
form.addEventListener('change', show);
form.addEventListener('reset', () => setTimeout(show));
show();
</script>

Best practices

  • In most forms, start each radio group with one checked button. A group with nothing checked is left out of the submitted data.
  • Wrap each checkbox in a <label>, or link one with for, so clicking the text toggles the box too. The larger target helps most on small screens such as phones.
  • Mark a box that must be ticked, such as one confirming the terms, with required. An unticked required box keeps the form from submitting.
  • Treat a missing checkbox field as unticked on the server. When a value has to arrive either way, add a hidden input from script.

Accessibility

Let the checked attribute and property carry the state, not ARIA. ARIA in HTML says authors must not put aria-checked on checkbox or radio inputs, because browsers expose the native checked state instead. In Chrome, an unticked box with aria-checked="true" is still exposed as unchecked.

Frequently asked questions

What makes a checkbox checked by default in HTML?
The checked attribute on the checkbox's input tag makes the box start ticked. It needs no value, and the user can still untick the box.
How do I set a checkbox to checked with JavaScript?
Set the checkbox's checked property, as in box.checked = true, which changes the state without firing a change event. Calling box.click() instead toggles the box and fires click, input and change.
Why does setAttribute with checked sometimes not tick the box?
The checked attribute stops moving the tick once the user toggles the box or a script sets the property. Set the checked property instead, or reset the form so the attribute takes over again.
Does checked=false uncheck a checkbox?
No, checked is a boolean attribute, so any value, false included, leaves the box ticked. True and false aren't valid values, so leave the attribute out to start the box unticked.
Does checked set the default on a radio button as it does on a checkbox?
Yes, put checked on one radio button in the group. If several carry it, later ones override earlier ones, so the last of them ends up checked.
Which CSS selector matches a checked checkbox?
The :checked pseudo-class matches boxes that are ticked right now, whatever the checked attribute says. The :default pseudo-class matches boxes that carry the attribute, ticked or not.
What does a checked checkbox send when the form is submitted?
A checked box sends its name and value, or the string on when it has no value. An unchecked box sends nothing, so the server sees no field for it.