Definition and Usage
The type="checkbox" attribute of the <input> tag represents a checkbox.
It allows a single value to be checked or unchecked. It is generally square, but its corners may be rounded.
Basic Example
<label for="opt-in">I agree to receive emails</label>
<input type="checkbox" id="opt-in">
The implicit role value of <input type="checkbox"> is checkbox.
Using Related Attributes
Let's learn about the attributes related to <input type="checkbox">.
checked Attribute
The checked attribute indicates whether the checkbox is checked by default. Use the checked attribute to have the checkbox start in a checked state when the page first loads. However, the checked attribute does not change when the user checks or unchecks the checkbox after the page has loaded.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<input type="checkbox" checked>
disabled Attribute
Use the disabled attribute to disable the checkbox.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<input type="checkbox" disabled>
required Attribute
The required attribute indicates that the checkbox is a required control that the user must check before submitting the form. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<form>
<label for="opt-in">I agree to receive emails (required)</label>
<!-- Specifies a required field using the required attribute -->
<input type="checkbox" id="opt-in" required>
<button type="submit">Submit</button>
</form>
You will see the browser display an error message for a required field.
value Attribute
value attribute sets the initial value.
This value is sent upon form submission only if the element is checked. If no value is specified, the default string on is submitted when the element is checked.
<form>
<label for="opt-in">I agree to receive emails</label>
<input type="checkbox" id="opt-in" name="opt-in" value="email-checked">
<button type="submit">Submit</button>
</form>
name Attribute
The name attribute specifies the name of the element.
When the name attribute is specified, its value is used as the name of the data sent when the form is submitted. In JavaScript, the value of the name attribute is also used as the name of the element in the form.elements API.
Grouping Multiple Checkboxes
The name attribute can be used to group multiple <input type="checkbox"> elements together.
When multiple checkboxes share the same name value, the values of all selected checkboxes are submitted under that single name.
Grouping related checkboxes into a set makes it easier to identify which options within a specific group were selected, both from the markup structure and the submitted data.
The code examples we've seen so far have dealt with a single checkbox. Let's look at code for handling multiple checkboxes.
The following example has multiple checkboxes that allow the user to select the web development languages they are interested in. As shown in the example, you can group the checkboxes using the <fieldset> element. In this case, the grouped checkboxes have the same name attribute and different value attributes. Although they are grouped together, the user is not restricted to selecting just one checkbox—they can select as many checkboxes as they like.
<fieldset>
<legend>Select the web development languages you are interested in.</legend>
<div>
<input type="checkbox" id="html" name="favorite" value="html">
<label for="html">HTML</label>
</div>
<div>
<input type="checkbox" id="css" name="favorite" value="css">
<label for="css">CSS</label>
</div>
<div>
<input type="checkbox" id="js" name="favorite" value="js">
<label for="js">JavaScript</label>
</div>
</fieldset>
Code Explanation
The <legend> element is used to provide a caption (a brief title or description) for the elements grouped by the parent <fieldset> element.
Differences from Radio Buttons
Radio buttons are similar to checkboxes, but there are some important differences.
Radio buttons are represented by <input type="radio">.
Differences in Checking and Unchecking a Single Control
| Checkbox | You can alternately check and uncheck a single checkbox. |
|---|---|
| Radio Button | You cannot alternately check and uncheck a single radio button. |
<input type="checkbox">
<input type="radio">
Differences in Grouping
When grouping checkboxes and radio buttons, they share a common requirement: grouped items must have the same name attribute value, while each item must have a distinct value attribute value.
However, grouped checkboxes allow the user to select multiple options, whereas grouped radio buttons allow only one option to be selected.
Grouped Checkboxes
<fieldset>
<legend>Select the web development languages you are interested in.</legend>
<div>
<input type="checkbox" id="html" name="interest" value="html">
<label for="html">HTML</label>
</div>
<div>
<input type="checkbox" id="css" name="interest" value="css">
<label for="css">CSS</label>
</div>
<div>
<input type="checkbox" id="js" name="interest" value="js">
<label for="js">JavaScript</label>
</div>
</fieldset>
Grouped Radio Buttons
<fieldset>
<legend>Select the web development languages you are interested in.</legend>
<div>
<input type="radio" id="html" name="interest" value="html">
<label for="html">HTML</label>
</div>
<div>
<input type="radio" id="css" name="interest" value="css">
<label for="css">CSS</label>
</div>
<div>
<input type="radio" id="js" name="interest" value="js">
<label for="js">JavaScript</label>
</div>
</fieldset>
Accessibility Considerations
When using <input type="checkbox">, you should use it together with a <label> tag.
For more information, see Accessibility Considerations for the <input> Tag.
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<input type="checkbox"> |
1 | 12 | 1 | 1 |
checked
|
1 | 12 | 1 | 1 |
disabled
|
1 | 12 | 1 | 1 |
name
|
1 | 12 | 1 | 1 |
value
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
<input type="checkbox">
|
HTML Standard #checkbox-state-(type=checkbox) |