Hello friends, In this post you will learn, how to get checkbox value from child component to parent lwc component, let's get started,
Checkbox value from child to parent in lightning web component
To implement this example you have to create two lwc component. One child component that contains checkbox field and second one is parent component that get the checkbox value from child component and display the same.
ChildComponent.html
<template>ChildComponent.js
<div style="width:100%;padding:1%">
<lightning-input type="checkbox" label="Select" checked={inputChk} onchange={chkChangeHandler}>
</lightning-input>
</div>
</template>
import { LightningElement } from 'lwc';ChildComponent.js-meta.xml
export default class ChildComponent extends LightningElement {
inputChk;
chkChangeHandler(event) {
this.inputChk = event.target.checked;
const selectedEvent = new CustomEvent("valuechange", {
detail: this.inputChk
});
this.dispatchEvent(selectedEvent);
}
}
<?xml version="1.0"?>ParentComponent.html
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<isExposed>true</isExposed>
</LightningComponentBundle>
<template>ParentComponent.js
<lightning-card title="Checkbox value from child to parent in Lightning Web Components" icon-name="action:announcement">
<c-child-component onvaluechange={childChkHandler}></c-child-component>
<div style="margin-left:10px;">
<h1>{chkValue}</h1>
</div>
</lightning-card>
</template>
import { LightningElement } from 'lwc';ParentComponent.js-meta.xml
export default class ParentComponent extends LightningElement {
chkValue;
childChkHandler(event) {
this.chkValue=event.detail;
}
}
<?xml version="1.0"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__Tab</target>
</targets>
</LightningComponentBundle>
Output:
Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.
0 Comments
Post a Comment