How to make a lightning checkbox?
To make a checkbox in LWC checked by default it has to have the "checked" attribute inside the markup and it is not responding on placing value=true or value= false inside the markup. But is there any way to assign a variable that would store the value.
lightning-input class="slds-p-left_xx-large" type="checkbox" label="" onchange={handleChange} checked
I also tried to make the "checked" attribute a dynamic string variable but that too didn't work.
Basically I want to Select all checkbox clicks on which it will select the other checkboxes. But I am unable to assign any dynamic value to check and uncheck the boxes. Is there any workaround to it for LWC?
You can add attributes checked to lightning-input from the javascript controller. You can access the lightning checkbox element by some attribute. In this example all checkboxes are accessed by setting the data-id attribute.
html file - js controller-
import { LightningElement, track, api } from 'lwc';
export default class lwcCmpName extends LightningElement {
handleChange(event) {
let i;
let checkboxes = this.template.querySelectorAll('[data-id="checkbox"]')
for(i=0; i
Here when Checkbox Select All will be checked all other checkboxes will be checked and same for unchecked.