Checkbox
Checkboxes allow users to select one or more options from a set. Each checkbox can toggle between checked and unchecked states.
Usage
Import the component:
import 'mdui/components/checkbox.js';
Import the TypeScript type:
import type { Checkbox } from 'mdui/components/checkbox.js';
Example:
<mdui-checkbox>Checkbox</mdui-checkbox>Examples
Checked State
The checked property is true when the checkbox is checked. Add the checked attribute to set the checkbox to the checked state by default.
Disabled State
Use the disabled attribute to disable the checkbox.
Indeterminate State
The indeterminate attribute sets the checkbox to an indeterminate state.
Icons
Use the unchecked-icon, checked-icon, and indeterminate-icon attributes to set the Material Icons for the checkbox in unchecked, checked, and indeterminate states, respectively. Alternatively, use the corresponding slots to set the icons.
API
Properties
| Attribute | Property | Reflect | Type | Default |
|---|---|---|---|---|
disabled | disabled | boolean | false | |
Disables the checkbox. | ||||
checked | checked | boolean | false | |
Sets the checkbox to the checked state. | ||||
defaultChecked | boolean | false | ||
Sets the default checked state. The checkbox resets to this state when the form is reset. JavaScript only. | ||||
indeterminate | indeterminate | boolean | false | |
Sets the checkbox to an indeterminate state. | ||||
required | required | boolean | false | |
The checkbox must be checked to submit the form. | ||||
form | form | string | - | |
Associates the checkbox with a This lets the checkbox work with any form in the document, not just the one it is nested in. | ||||
name | name | string | '' | |
Sets the checkbox's name, which is submitted with form data. | ||||
value | value | string | 'on' | |
Sets the checkbox's value, which is submitted with form data. | ||||
unchecked-icon | uncheckedIcon | string | - | |
Sets the Material Icons name for the unchecked state. Alternatively, use | ||||
checked-icon | checkedIcon | string | - | |
Sets the Material Icons name for the checked state. Alternatively, use | ||||
indeterminate-icon | indeterminateIcon | string | - | |
Sets the Material Icons name for the indeterminate state. Alternatively, use | ||||
validity | ValidityState | - | ||
A | ||||
validationMessage | string | - | ||
Validation message. Empty string when valid. | ||||
autofocus | autofocus | boolean | false | |
Whether the element is focused when the page loads. | ||||
tabindex | tabIndex | number | - | |
The element's tab order when navigating with the Tab key. | ||||
Methods
| Name | Parameters | Returns |
|---|---|---|
checkValidity | boolean | |
Checks the validity of the form field. If it is invalid, it triggers an | ||
reportValidity | boolean | |
Checks the validity of the form field. If it is invalid, it triggers an | ||
setCustomValidity |
| void |
Sets a custom error message. If the message is non-empty, the field is considered invalid. | ||
click | void | |
Simulates a mouse click on the element. | ||
focus |
| void |
Sets focus on the element. An optional object parameter may include a | ||
blur | void | |
Removes focus from the element. | ||
Slots
| Name |
|---|
| (default) |
Text. |
unchecked-icon |
Icon for the unchecked state. |
checked-icon |
Icon for the checked state. |
indeterminate-icon |
Icon for the indeterminate state. |
CSS Parts
| Name |
|---|
control |
Container for the left icon. |
unchecked-icon |
Icon for the unchecked state. |
checked-icon |
Icon for the checked state. |
indeterminate-icon |
Icon for the indeterminate state. |
label |
Text. |