Hey guys, today in this post we are going to learn about How to get checkbox value in lwc component and display the selected value using of “lightning-checkbox-group” property in Lightning Web Component (LWC) Salesforce.
A lightning-checkbox-group component represents a checkbox group that enables selection of single or multiple options.
If the required attribute is set, at least one checkbox must be selected. When a user interacts with the checkbox group and doesn’t make a selection, an error message is displayed. You can provide a custom error message using the message-when-value-missing attribute.
If the disabled attribute is set, checkbox selections can’t be changed.
This example creates a checkbox group with three options and option1 is selected by default. To read more about lightning-checkbox-group, click Here..
Files we used to get selected checkbox value in LWC
lwcCheckboxValue.html | LWc HTML File | Template HTML file to get selected checkbox value in Salesforce Lightning Web Component (LWC) |
lwcCheckboxValue.js | LWC JavaScript File | Itβs hold a javascript on Handle handleChange function to get the value of selected checkbox in lwc. |
lwcCheckboxValue.js-meta.xml | XML Meta File | It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage. |
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps
Create Lightning Web Component HTML
Step 1:- Create Lightning Web Component HTML β‘ lwcCheckboxValue.html
SFDX:Lightning Web Component β‘ New β‘ lwcCheckboxValue.html
lwcCheckboxValue.html [Lightning Web Component HTML]
<template>
<div class="slds-p-around--medium">
<h3 class="slds-text-heading_medium"><strong style="color:#270086;">Get checkbox value and display the selected value in Lightning Web Component (LWC) </strong></h3>
<br/>
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_2-of-12">
<lightning-checkbox-group name="Checkbox Group"
label="Checkbox Group"
options={options}
value={value}
onchange={handleChange}></lightning-checkbox-group>
</div>
<div class="slds-col slds-size_10-of-12">
<template if:true={integrationVal}>
<h3 class="slds-text-heading_medium slds-text-color--error"><strong>Salesforce IntegrationVal</strong></h3>
<p>How to Integrate Amazon S3 into Salesforce Uses of Named Credentials and checked the results Status=OK, StatusCode=200 after connected Amazon AWS S3 through the anonymous window in Salesforce | Integrate aws s3 using named credentials in Salesforce. <span class="readMore"><a href="https://www.w3web.net/aws-s3-integration-step-by-step-in-salesforce/" target="_blank" rel="noopener noreferrer">more...</a></span></p>
</template>
<template if:true={salesforceLWCVal}>
<h3 class="slds-text-heading_medium slds-text-color--error"><strong>Salesforce LWC</strong></h3>
<p>Retrieve picklist values dynamically of Opportunity Object without apex class using βuiObjectInfoApiβ property and displaying the selected pick-list value in Salesforce Lightning Component β LWC. <span class="readMore"><a href="https://www.w3web.net/lwc-fetch-picklist-values-without-apex-in-salesforce/" target="_blank" rel="noopener noreferrer">more...</a></span></p>
</template>
<template if:true={auraComponentVal}>
<h3 class="slds-text-heading_medium slds-text-color--error"><strong>Aura Component</strong></h3>
<p>The Lightning Component framework is a UI framework for developing web apps for mobile and desktop devices. It's a modern framework for building single-page applications with dynamic, responsive user interfaces for Lightning Platform apps. To live demo project learn <span class="readMore"><a href="https://www.w3web.net/tutorial/lightning-component/" target="_blank" rel="noopener noreferrer">more...</a></span></p>
</template>
</div>
</div>
<br/><br/>
<p style="color: #ff0000;">Selected Values are: <strong>{value}</strong></p>
<br/><br/>
Read more about <a href="https://www.w3web.net/tutorial/salesforce-lwc/" target="_blank" rel="noopener noreferrer">Salesforce LWC</a>
</div>
<br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
<br/><br/>
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like β</span> </strong></p>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
<li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
<li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
<li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
<li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
<li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
<li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</lightning-card>
</template>
</template>
Create Lightning Web Component Javascript
Step 2:- Create Lightning Web Component Javascript β‘ lwcCheckboxValue.js
SFDX:Lightning Web Component β‘ New β‘ lwcCheckboxValue.js
lwcCheckboxValue.js [LWC JavaScript File]
import { LightningElement,track } from 'lwc';
export default class LwcCheckboxValue extends LightningElement {
value = 'integration';
get options() {
return [
{ label: 'Salesforce Integration', value: 'integration' },
{ label: 'Salesforce LWC', value: 'salesforceLWC' },
{ label: 'Aura Component', value: 'auraComponent' },
];
}
@track integrationVal = true;
@track salesforceLWCVal = false;
@track auraComponentVal = false;
handleChange(event) {
this.value = event.detail.value;
//alert('value!!! ' + this.value);
if (this.value == 'integration'){
this.integrationVal = true;
}else{
this.integrationVal = false;
}
if (this.value == 'salesforceLWC'){
this.salesforceLWCVal = true;
}else{
this.salesforceLWCVal = false;
}
if (this.value == 'auraComponent'){
this.auraComponentVal = true;
}else{
this.auraComponentVal = false;
}
}
}
Create Lightning Web Component Meta XML
Step 3:- Create Lightning Web Component Meta XML β‘ lwcCheckboxValue.js-meta.xml
SFDX:Lightning Web Component β‘ New >> lwcCheckboxValue.js-meta.xml
lwcCheckboxValue.js-meta.xml [LWC Meta Data XML]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Further post that would you like to learn in Salesforce
What is the difference between checkbox and option button?
In Check box, you can select multiple options. In Option Button (Radio button) you can select one option. Radio buttons are circular and check boxes are square.
Can radio button be unchecked?
Radio button helps in ensuring only one option is selected. However, it doesn't allow user to deselect the option.
What is checkbox?
The CheckBox control allows the user to set true/false or yes/no type options. The user can select or deselect it.
Related Topics | You May Also Like
Our Free Courses β
π Get Free Course β
![]() π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β
![]() π Aura Lightning Framework π Lightning Web Component (LWC) π Rest APIs Integration |