Please refer the code I have written for you, it should make sense if not ask me.
Your html for one or more check box
<template>
For multiple Checkbox use Checkbox Group
<lightning-checkbox-group name="Checkbox Group"
label="Checkbox Group"
options={options}
value={value}
onchange={handleChange}></lightning-checkbox-group>
<p>Selected Values are: {selectedValues}</p>
for just single Checkbox
<input type="checkbox" name="vehicle1" value="Bike" id="mycheck" onclick={myFunction}> I have a bike<br>
<p>Selected:</p> {checkvalue}
</template>
Your js to handle that, For single checkbox right now it assign value (you actuallu asked for ) to check box to keep it simple you can modify it to assign true false depending on last value.
import { LightningElement, track } from 'lwc';
export default class CheckboxGroupBasic extends LightningElement {
@track value = ['option1'];
@track checkvalue ;
get options() {
return [
{ label: 'Ross', value: 'option1' },
{ label: 'Rachel', value: 'option2' },
];
}
get selectedValues() {
return this.value.join(',');
}
handleChange(e) {
this.value = e.detail.value;
}
myFunction(e){ // it is simple assigning value. here you can toggle value
this.checkvalue = e.target.value;
}
}
We have LWC Playground link you want to see it working.
https://developer.salesforce.com/docs/component-library/tools/playground/1_UbRgnJ9/9/edit