Checkbox
<wa-checkbox>
Checkboxes allow the user to toggle an option on or off.
<wa-checkbox>Checkbox</wa-checkbox>
This component works with standard <form> elements. Please refer to the section on form controls to learn more about form submission and client-side validation.
Examples
Jump to heading
Checked
Jump to heading
Use the checked attribute to activate the checkbox.
<wa-checkbox checked>Checked</wa-checkbox>
Indeterminate
Jump to heading
Use the indeterminate attribute to make the checkbox indeterminate.
<wa-checkbox indeterminate>Indeterminate</wa-checkbox>
Disabled
Jump to heading
Use the disabled attribute to disable the checkbox.
<wa-checkbox disabled>Disabled</wa-checkbox>
Sizes
Jump to heading
Use the size attribute to change a checkbox's size.
<wa-checkbox size="small">Small</wa-checkbox> <br /> <wa-checkbox size="medium">Medium</wa-checkbox> <br /> <wa-checkbox size="large">Large</wa-checkbox>
Hint
Jump to heading
Add descriptive hint to a switch with the hint attribute. For hints that contain HTML, use the hint slot instead.
<wa-checkbox hint="What should the user know about the checkbox?">Label</wa-checkbox>
Custom Validity
Jump to heading
Use the setCustomValidity() method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string.
<form class="custom-validity"> <wa-checkbox>Check me</wa-checkbox> <br /> <wa-button type="submit" variant="brand" style="margin-top: 1rem;">Submit</wa-button> </form> <script> const form = document.querySelector('.custom-validity'); const checkbox = form.querySelector('wa-checkbox'); const errorMessage = `Don't forget to check me!`; // Set initial validity as soon as the element is defined customElements.whenDefined('wa-checkbox').then(async () => { await checkbox.updateComplete; checkbox.setCustomValidity(errorMessage); }); // Update validity on change checkbox.addEventListener('change', () => { checkbox.setCustomValidity(checkbox.checked ? '' : errorMessage); }); // Handle submit customElements.whenDefined('wa-checkbox').then(() => { form.addEventListener('submit', event => { event.preventDefault(); alert('All fields are valid!'); }); }); </script>
Slots
Jump to heading
Learn more about using slots.
| Name | Description |
|---|---|
| (default) | The checkbox's label. |
hint
|
Text that describes how to use the checkbox. Alternatively, you can use the hint attribute. |
Attributes & Properties
Jump to heading
Learn more about attributes and properties.
| Name | Description | Reflects | |
|---|---|---|---|
namename |
The name of the checkbox, submitted as a name/value pair with form data.
Type
stringDefault
'' |
|
|
valuevalue |
The value of the checkbox, submitted as a name/value pair with form data.
Type
string | null |
|
|
sizesize |
The checkbox's size.
Type
'small' | 'medium' | 'large'Default
'medium' |
|
|
disableddisabled |
Disables the checkbox.
Type
booleanDefault
false |
||
indeterminateindeterminate |
Draws the checkbox in an indeterminate state. This is usually applied to checkboxes that represents a "select
all/none" behavior when associated checkboxes have a mix of checked and unchecked states.
Type
booleanDefault
false |
|
|
checked |
Draws the checkbox in a checked state.
Type
boolean |
||
defaultCheckedchecked |
The default value of the form control. Primarily used for resetting the form control.
Type
boolean |
|
|
formform |
By default, form controls are associated with the nearest containing
<form> element. This attribute allows you
to place the form control outside of a form and associate it with the form that has this id. The form must be in
the same document or shadow root for this to work.Type
nullDefault
null |
|
|
requiredrequired |
Makes the checkbox a required field.
Type
booleanDefault
false |
|
|
hinthint |
The checkbox's hint. If you need to display HTML, use the
hint slot instead.Type
stringDefault
'' |
Methods
Jump to heading
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
click() |
Simulates a click on the checkbox. | |
focus() |
Sets focus on the checkbox. |
options: FocusOptions
|
blur() |
Removes focus from the checkbox. |
Events
Jump to heading
Learn more about events.
| Name | Description |
|---|---|
change |
Emitted when the checked state changes. |
blur |
Emitted when the checkbox loses focus. |
focus |
Emitted when the checkbox gains focus. |
input |
Emitted when the checkbox receives input. |
wa-invalid |
Emitted when the form control has been checked for validity and its constraints aren't satisfied. |
CSS custom properties
Jump to heading
Learn more about CSS custom properties.
| Name | Description |
|---|---|
--checked-icon-color |
The color of the checked and indeterminate icons.
|
--checked-icon-scale |
The size of the checked and indeterminate icons relative to the checkbox.
|
Custom States
Jump to heading
Learn more about custom states.
| Name | Description | CSS selector |
|---|---|---|
checked |
Applied when the checkbox is checked. |
:state(checked)
|
disabled |
Applied when the checkbox is disabled. |
:state(disabled)
|
indeterminate |
Applied when the checkbox is in an indeterminate state. |
:state(indeterminate)
|
CSS parts
Jump to heading
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
base |
The component's label . |
::part(base)
|
control |
The square container that wraps the checkbox's checked state. |
::part(control)
|
checked-icon |
The checked icon, a <wa-icon> element. |
::part(checked-icon)
|
indeterminate-icon |
The indeterminate icon, a <wa-icon> element. |
::part(indeterminate-icon)
|
label |
The container that wraps the checkbox's label. |
::part(label)
|
hint |
The hint's wrapper. |
::part(hint)
|
Dependencies
Jump to heading
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.
Importing
Jump to heading
Autoloading components via projects is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.
Let your project code do the work! Sign up for free to use a project with your very own CDN — it's the fastest and easiest way to use Web Awesome.
To manually import this component from NPM, use the following code.
import '@awesome.me/webawesome/dist/components/checkbox/checkbox.js';
To manually import this component from React, use the following code.
import WaCheckbox from '@awesome.me/webawesome/dist/react/checkbox';