lightning-input implements designs in the Salesforce Lightning Design System (SLDS). The input types adapt to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.
When working with checkboxes and toggle
switches, use this.template.querySelectorAll to retrieve the array of components. You can
use .filter to determine which elements are checked or unchecked. The following
example displays the values of the selected checkboxes.
When you select a checkbox, the handleCheckboxChange function updates the selection property
to display a list of selected checkboxes.
1import{LightningElement}from "lwc";23export default class CheckboxExample extends LightningElement{4 selection;56 handleCheckboxChange(){7 // Query the DOM8 const checked = Array.from(this.template.querySelectorAll("lightning-input"))9 // Filter down to checked items10 .filter((element)=> element.checked)11 // Map checked items to their labels12 .map((element)=> element.label);13 this.selection = checked.join(", ");14}15}
To programmatically set a checkbox or checkbox button to checked, query the element using a custom data attribute. You can’t query the internal elements of a Lightning web component. This example uses a custom attribute data-element to query the element. The checkbox is selected by clicking a button.
To validate the checkbox button, use the checkValidity() method. If the value isn’t valid, the reportValidity() method shows the error message below the checkbox button.
1import{LightningElement}from "lwc";23export default class CheckboxButtonValidation extends LightningElement{4 handleSubmit(){5 const checkboxButton = this.refs.termsCheckbox;6 console.log(checkboxButton.validity.valid); // Returns true or false7 if(!checkboxButton.checkValidity()){8 checkboxButton.reportValidity();9 return;10}11 // Proceed with form submission12}13}
The validity attribute returns an object with read-only boolean properties. For the checkbox-button type, these attributes apply:
badInput - Indicates that the value is invalid for any input type
customError - Indicates that a custom error has been set using setCustomValidity()
valueMissing - Indicates that an empty value is provided when the required attribute is set for any input type
valid - True if none of the preceding properties are true
Usage Considerations
The label is always hidden from view and available as assistive text for screen readers. Using the variant attribute with the checkbox button has no impact on label display or layout.
Field-level help text isn’t supported for the checkbox button.
Accessibility
When you use the label attribute, the component generates a unique ID for the internal <label> and uses a standard for attribute to link it to the checkbox button.
If you use the label-hidden variant, the component maintains the for attribute to link between the checkbox button and the label.
If you use the field-level-help attribute, the component creates an aria-describedby link between the checkbox button and the help tooltip.
If the checkbox button fails validation, the component adds aria-invalid="true" and links the error message to the input by using aria-describedby.