How to get/set checkbox value in lwc component and display the selected value using of “lightning-checkbox-group” property in Lightning Web Component (LWC) Salesforce | how to get selected checkbox value in lwc

12,689 views

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

get selected checkbox value in lwc -- w3web.net

 
 

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]

  1.    <template>
  2. <div class="slds-p-around--medium">
  3.     <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> 
  4.     <br/>
  5.     <div class="slds-grid slds-wrap">
  6.         <div class="slds-col slds-size_2-of-12">
  7.             <lightning-checkbox-group name="Checkbox Group"
  8.                                 label="Checkbox Group"
  9.                                 options={options}
  10.                                 value={value}
  11.                                 onchange={handleChange}></lightning-checkbox-group>
  12.                 </div>      
  13.  
  14.  
  15.                 <div class="slds-col slds-size_10-of-12">
  16.                 <template if:true={integrationVal}>
  17.                     <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Salesforce IntegrationVal</strong></h3> 
  18.                     <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>
  19.  
  20.                 </template>
  21.  
  22.  
  23.                 <template if:true={salesforceLWCVal}>
  24.                     <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Salesforce LWC</strong></h3> 
  25.                     <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>
  26.  
  27.                 </template>
  28.  
  29.  
  30.                 <template if:true={auraComponentVal}>
  31.                     <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Aura Component</strong></h3> 
  32.                     <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> 
  33.  
  34.                 </template>
  35.  
  36.  
  37.                 </div>
  38.     </div>                      
  39.     <br/><br/>
  40.  
  41.      <p style="color: #ff0000;">Selected Values are: <strong>{value}</strong></p>       
  42.     <br/><br/>
  43.   Read more about <a href="https://www.w3web.net/tutorial/salesforce-lwc/" target="_blank" rel="noopener noreferrer">Salesforce LWC</a>
  44. </div>
  45. <br/>
  46.    <!--Start RelatedTopics Section-->
  47. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  48.  
  49.             <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>
  50.  
  51.             <br/><br/>
  52.             <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>
  53.             <div style="display:block; overflow:hidden;"> 
  54.                 <div style="width: 50%; float:left; display:inline-block">
  55.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  56.                         <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>
  57.                         <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>
  58.                         <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>
  59.                         <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>
  60.                         <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>
  61.                     </ul>
  62.             </div>
  63.  
  64.             <div style="width: 50%; float:left; display:inline-block">
  65.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  66.                         <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>
  67.                         <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>
  68.                         <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>
  69.                         <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>
  70.                         <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>
  71.                     </ul>
  72.                 </div>
  73.                <div style="clear:both;"></div> 
  74.                <br/>
  75.                 <div class="youtubeIcon">
  76.                     <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>
  77.                 </div>
  78.     </div>
  79.  
  80. </div>
  81.  
  82.   <!--End RelatedTopics Section-->
  83.  
  84.    </lightning-card> 
  85. </template>
  86.  
  87. </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]

  1.    import { LightningElement,track } from 'lwc';
  2.  
  3. export default class LwcCheckboxValue extends LightningElement {
  4.  
  5.     value = 'integration';
  6.  
  7.     get options() {
  8.         return [
  9.             { label: 'Salesforce Integration', value: 'integration' },
  10.             { label: 'Salesforce LWC', value: 'salesforceLWC' },
  11.             { label: 'Aura Component', value: 'auraComponent' },
  12.         ];
  13.     }
  14.  
  15.     @track integrationVal = true;
  16.     @track salesforceLWCVal = false;
  17.     @track auraComponentVal = false;
  18.  
  19.     handleChange(event) {
  20.  
  21.         this.value = event.detail.value;        
  22.         //alert('value!!! ' + this.value);
  23.         if (this.value == 'integration'){
  24.             this.integrationVal = true;
  25.         }else{
  26.             this.integrationVal = false;
  27.         }
  28.  
  29.         if (this.value == 'salesforceLWC'){
  30.             this.salesforceLWCVal = true;
  31.         }else{
  32.             this.salesforceLWCVal = false;
  33.         }
  34.  
  35.         if (this.value == 'auraComponent'){
  36.             this.auraComponentVal = true;
  37.         }else{
  38.             this.auraComponentVal = false;
  39.         }
  40.     }
  41.  
  42. }

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]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.       </targets>
  10. </LightningComponentBundle>

get selected checkbox value in lwc -- w3web.net

Further post that would you like to learn in Salesforce

 

 

 

 

FAQ (Frequently Asked Questions)

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

 
 


Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment