e01.org

`<input type="checkbox">` HTML attribute value

input elements of type checkbox are rendered by default as boxes that are checked (ticked) when activated, like you might see in an official government paper form. The exact appearance depends upon the operating system configuration under which the browser is running. Generally this is a square but it may have rounded corners. A checkbox allows you to select single values for submission in a form

input elements of type **`checkbox`** are rendered by default as boxes that are checked (ticked) when activated, like you might see in an official government paper form. The exact appearance depends upon the operating system configuration under which the browser is running. Generally this is a square but it may have rounded corners. A checkbox allows you to select single values for submission in a form (or not).

tabbed-standard

```html interactive-example
<fieldset>
<legend>Choose your monster's features:</legend>

<div>
<input type="checkbox" id="scales" name="scales" checked />
<label for="scales">Scales</label>
</div>

<div>
<input type="checkbox" id="horns" name="horns" />
<label for="horns">Horns</label>
</div>
</fieldset>
```

```css interactive-example
p,
label {
font:
1rem "Fira Sans",
sans-serif;
}

input {
margin: 0.4rem;
}
```

## Value

A string representing the value of the checkbox. This is not displayed on the client-side, but on the server this is the `value` given to the data submitted with the checkbox's `name`. Take the following example:

```html
<form>
<div>
<input
type="checkbox"
id="subscribeNews"
name="subscribe"
value="newsletter" />
<label for="subscribeNews">Subscribe to newsletter?</label>
</div>
<div>
<button type="submit">Subscribe</button>
</div>
</form>
```

In this example, we've got a name of `subscribe`, and a value of `newsletter`. When the form is submitted, the data name/value pair will be `subscribe=newsletter`.

If the `value` attribute was omitted, the default value for the checkbox is `on`, so the submitted data in that case would be `subscribe=on`.

> [!NOTE]
> If a checkbox is unchecked when its form is submitted, neither the name nor the value is submitted to the server. There is no HTML-only method of representing a checkbox's unchecked state (e.g., `value=unchecked`). If you wanted to submit a default value for the checkbox when it is unchecked, you could include JavaScript to create an &lt;input type="hidden"&gt; within the form with a value indicating an unchecked state.

## Additional attributes

In addition to the [common attributes](/docs/Web/HTML/Reference/Elements/input#attributes) shared by all input elements, `checkbox` inputs support the following attributes.

- `checked`
- : A [boolean](/docs/Glossary/Boolean/HTML) attribute indicating whether this checkbox is checked by default (when the page loads). It does _not_ indicate whether this checkbox is currently checked: if the checkbox's state is changed, this content attribute does not reflect the change. (Only the HTMLInputElement's `checked` IDL attribute is updated.)
> [!NOTE]
> Unlike other input controls, a checkbox's value is only included in the submitted data if the checkbox is currently `checked`. If it is, then the value of the checkbox's `value` attribute is reported as the input's value, or `on` if no `value` is set.
> Unlike other browsers, Firefox by default [persists the dynamic checked state](https://stackoverflow.com/questions/5985839/bug-with-firefox-disabled-attribute-of-input-not-resetting-when-refreshing) of an `<input>` across page loads. Use the [`autocomplete`](/docs/Web/HTML/Reference/Elements/input#autocomplete) attribute to control this feature.

- `value`
- : The `value` attribute is one which all inputs share; however, it serves a special purpose for inputs of type `checkbox`: when a form is submitted, only checkboxes which are currently checked are submitted to the server, and the reported value is the value of the `value` attribute. If the `value` is not otherwise specified, it is the string `on` by default. This is demonstrated in the section [Value](#value) above.

- `switch`
- : A [boolean](/docs/Glossary/Boolean/HTML) attribute that applies only to `checkbox` inputs. When present, it indicates that the `checkbox` represents an on/off `switch` rather than a normal `checkbox`. It alters the appearance of the `checkbox` control, but the underlying behavior remains the same as that of a normal `checkbox`.

> [!NOTE]
> This attribute allows user agents to expose `switch` ARIA semantics to assistive technologies — without requiring documents to explicitly specify `role="switch"`. The markup and API are similar to those of checkboxes, except that the `:indeterminate` pseudo-class never matches.

> [!WARNING]
> This attribute is still experimental and has limited browser support. The attribute is ignored on unsupported browsers.

## Using checkbox inputs

Checkboxes are similar to [radio buttons](/docs/Web/HTML/Reference/Elements/input/radio), but with an important distinction — [same-named radio buttons](/docs/Web/HTML/Reference/Elements/input/radio#defining_a_radio_group) are grouped into a set in which only one radio button can be selected at a time, whereas checkboxes allow you to turn single values on and off. Where multiple same-named controls exist, radio buttons allow one to be selected out of them all, whereas checkboxes allow multiple values to be selected.

### Handling multiple checkboxes

The example we saw above only contained one checkbox; in real-world situations you'll be likely to encounter multiple checkboxes. If they are completely unrelated, then you can just deal with them all separately, as shown above. However, if they're all related, things are not quite so simple.

For example, in the following demo we include multiple checkboxes to allow the user to select their interests (see the full version in the [Examples](#examples) section).

```html
<fieldset>
<legend>Choose your interests</legend>
<div>
<input type="checkbox" id="coding" name="interest" value="coding" />
<label for="coding">Coding</label>
</div>
<div>
<input type="checkbox" id="music" name="interest" value="music" />
<label for="music">Music</label>
</div>
</fieldset>
```

600

In this example you will see that we've given each checkbox the same `name`. If both checkboxes are checked and then the form is submitted, you'll get a string of name/value pairs submitted like this: `interest=coding&interest=music`. When this string reaches the server, you need to parse it other than as an associative array, so all values, not only the last value, of `interest` are captured. For one technique used with Python, see [Handle Multiple Checkboxes with a Single Serverside Variable](https://stackoverflow.com/questions/18745456/handle-multiple-checkboxes-with-a-single-serverside-variable), for example.

### Checking boxes by default

To make a checkbox checked by default, you give it the `checked` attribute. See the below example:

```html
<fieldset>
<legend>Choose your interests</legend>
<div>
<input type="checkbox" id="coding" name="interest" value="coding" checked />
<label for="coding">Coding</label>
</div>
<div>
<input type="checkbox" id="music" name="interest" value="music" />
<label for="music">Music</label>
</div>
</fieldset>
```

600

### Switch as a checkbox

The following example shows how to make a checkbox look and act like an on/off switch.

```html
<fieldset>
<legend>Adjust your setting</legend>
<div>
<label for="theme">Dark mode</label>
<input type="checkbox" name="theme" id="theme" switch checked />
</div>
<div>
<label for="notifications">Notifications</label>
<input type="checkbox" name="notifications" id="notifications" switch />
</div>
</fieldset>
```

> [!NOTE]
> While only some browsers render the checkbox as a switch, the behavior is the same across all browsers.

600

### Providing a bigger hit area for your checkboxes

In the above examples, you may have noticed that you can toggle a checkbox by clicking on its associated label element as well as on the checkbox itself. This is a really useful feature of HTML form labels that makes it easier to click the option you want, especially on small-screen devices like smartphones.

Beyond accessibility, this is another good reason to properly set up `<label>` elements on your forms.

### Indeterminate state checkboxes

A checkbox can be in an **indeterminate** state. This is set using the HTMLInputElement object's [`indeterminate`](/docs/Web/API/HTMLInputElement/indeterminate) property via JavaScript (it cannot be set using an HTML attribute):

```js
inputInstance.indeterminate = true;
```

When `indeterminate` is `true`, the checkbox has a horizontal line in the box (it looks somewhat like a hyphen or minus sign) instead of a check/tick in most browsers.

> [!NOTE]
> This is purely a visual change. It has no impact on whether the checkbox's `value` is used in a form submission. That is decided by the `checked` state, regardless of the `indeterminate` state.

There are not many use cases for this property. The most common is when a checkbox is available that "owns" a number of sub-options (which are also checkboxes). If all of the sub-options are checked, the owning checkbox is also checked, and if they're all unchecked, the owning checkbox is unchecked. If any one or more of the sub-options have a different state than the others, the owning checkbox is in the indeterminate state.

This can be seen in the below example (thanks to [CSS Tricks](https://css-tricks.com/indeterminate-checkboxes/) for the inspiration). In this example we keep track of the ingredients we are collecting for a recipe. When you check or uncheck an ingredient's checkbox, a JavaScript function checks the total number of checked ingredients:

- If none are checked, the recipe name's checkbox is set to unchecked.
- If one or two are checked, the recipe name's checkbox is set to `indeterminate`.
- If all three are checked, the recipe name's checkbox is set to `checked`.

So in this case the `indeterminate` state is used to state that collecting the ingredients has started, but the recipe is not yet complete.

```js live-sample___indeterminate_state
const overall = document.querySelector("#enchantment");
const ingredients = document.querySelectorAll("ul input");

overall.addEventListener("click", (e) => {
e.preventDefault();
});

for (const ingredient of ingredients) {
ingredient.addEventListener("click", updateDisplay);
}

function updateDisplay() {
let checkedCount = 0;
for (const ingredient of ingredients) {
if (ingredient.checked) {
checkedCount++;
}
}

if (checkedCount === 0) {
overall.checked = false;
overall.indeterminate = false;
} else if (checkedCount === ingredients.length) {
overall.checked = true;
overall.indeterminate = false;
} else {
overall.checked = false;
overall.indeterminate = true;
}
}
```

```html live-sample___indeterminate_state
<fieldset>
<legend>Complete the recipe</legend>
<div>
<input type="checkbox" id="enchantment" name="enchantment" />
<label for="enchantment">Enchantment table</label>
<ul>
<li>
<input type="checkbox" id="book" name="ingredient" value="book" />
<label for="book">Book</label>
</li>
<li>
<input
type="checkbox"
id="diamonds"
name="ingredient"
value="diamonds" />
<label for="diamonds">Diamonds (x2)</label>
</li>
<li>
<input
type="checkbox"
id="obsidian"
name="ingredient"
value="obsidian" />
<label for="obsidian">Obsidian (x4)</label>
</li>
</ul>
</div>
</fieldset>
```

## Validation

Checkboxes do support [validation](/docs/Web/HTML/Guides/Constraint_validation) (offered to all inputs). However, most of the ValidityStates will always be `false`. If the checkbox has the [`required`](/docs/Web/HTML/Reference/Elements/input#required) attribute, but is not checked, then ValidityState.valueMissing will be `true`.

## Examples

The following example is an extended version of the "multiple checkboxes" example we saw above — it has more standard options, plus an "other" checkbox that when checked causes a text field to appear to enter a value for the "other" option. This is achieved with a short block of JavaScript. The example includes implicit labels, with the `<input>` directly inside the `<label>`. The text input, without a visible label, includes the [`aria-label`](/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-label) attribute which provides its accessible name. This example also includes some CSS to improve the styling.

### HTML

```html
<fieldset>
<legend>Choose your interests</legend>
<div>
<label>
<input type="checkbox" id="coding" name="interest" value="coding" />
Coding
</label>
</div>
<div>
<label>
<input type="checkbox" id="music" name="interest" value="music" />
Music
</label>
</div>
<div>
<label>
<input type="checkbox" id="art" name="interest" value="art" />
Art
</label>
</div>
<div>
<label>
<input type="checkbox" id="sports" name="interest" value="sports" />
Sports
</label>
</div>
<div>
<label>
<input type="checkbox" id="cooking" name="interest" value="cooking" />
Cooking
</label>
</div>
<div>
<label>
<input type="checkbox" id="other" name="interest" value="other" />
Other
</label>
<input
type="text"
id="otherValue"
name="other"
aria-label="Other interest" />
</div>
</fieldset>
```

### CSS

```css
html {
font-family: sans-serif;
}

div {
margin-bottom: 10px;
}

fieldset {
width: 600px;
margin: 0 auto;
background: cyan;
border: 5px solid blue;
}

legend {
padding: 10px;
background: blue;
color: cyan;
}
```

### JavaScript

```js
const otherCheckbox = document.querySelector("#other");
const otherText = document.querySelector("#otherValue");
otherText.style.visibility = "hidden";

otherCheckbox.addEventListener("change", () => {
if (otherCheckbox.checked) {
otherText.style.visibility = "visible";
otherText.value = "";
} else {
otherText.style.visibility = "hidden";
}
});
```

100%

## Technical summary

<table class="properties">
<tbody>
<tr>
<td><strong><a href="#value">Value</a></strong></td>
<td>
A string representing the value of the
checkbox.
</td>
</tr>
<tr>
<td><strong>Events</strong></td>
<td>change and input</td>
</tr>
<tr>
<td><strong>Supported common attributes</strong></td>
<td>
<code><a href="#checked">checked</a></code> and
<code><a href="#switch">switch</a></code>
</td>
</tr>
<tr>
<td><strong>IDL attributes</strong></td>
<td>
<code><a href="/en-US/docs/Web/API/HTMLInputElement/checked">checked</a></code>,
<code><a href="/en-US/docs/Web/API/HTMLInputElement/indeterminate">indeterminate</a></code> and
<code><a href="/en-US/docs/Web/API/HTMLInputElement/value">value</a></code>
</td>
</tr>
<tr>
<td><strong>DOM interface</strong></td>
<td><p>HTMLInputElement</p></td>
</tr>
<tr>
<td><strong>Implicit ARIA Role</strong></td>
<td><a href="/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/checkbox_role"><code>checkbox</code></a></td>
</tr>
</tbody>
</table>

## Specifications

## Browser compatibility

## See also

- :checked, :indeterminate: CSS selectors that let you style checkboxes based on their current state
- HTMLInputElement: HTML DOM API that implements the `<input>` element

source: MDN Web Docs mirror · page

Cite this

"` ` HTML attribute value". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/checkbox

JSON record · API collection · citation policy