LWC how to get selected radio button value on handle-change function and displaying content of radio button checked value Using ‘lightning-radio-group’ element in Lightning Web Component (LWC) | How to display selected radio button value in lwc

11,551 views

Hey guys, today in this post we are going to learn about LWC how to get selected radio button value on handle-change function and displaying content of radio button checked value Using ‘lightning-radio-group’ element in Lightning Web Component (LWC).

A lightning-radio-group component represents a group of radio buttons that permit only one button to be selected at a time. The component renders radio button input elements and assigns the same value to the name attribute for each element. The common name attribute joins the elements in a group. If you select any radio button in that group, any previously selected button in the group is deselected.

In general, we don’t recommend setting the name attribute in lightning-radio-group. The component automatically generates a unique value for name if none is provided. The generated value ensures a common name for the input elements rendered for the radio button group, and is unique in the page.

Why Should You Schedule Meeting?

🎯 If You Are Facing Any Of These 6 Challenges. Schedule Meeting With Me.

  • Learn Salesforce Development
  • Career Confusion
  • No Interview Call
  • Low Salary
  • No Promotion/Growth
  • No Finding New Job Opportunity
  • Why you stucking from past so many years in same company?

 

See Reusing lightning-radio-group in a Page for name attribute considerations if you want to use the component multiple times in a page.

If the required attribute is specified, at least one radio button must be selected. When a user interacts with the radio button group and doesn’t make a selection, an error message is displayed.

If the disabled attribute is specified, radio button selections can’t be changed. To learn more Click Here.

Files we used in this post example

lwcRadioGroup.html LWc HTML File Template HTML file to get selected radio button value in Salesforce Lightning Web Component (LWC)
lwcRadioGroup.js LWC JavaScript File It’s hold a javascript on handlechange function to get selected radio button value from current radio group button.
lwcRadioGroup.js-meta.xml XML Meta File It is used to where this lightning web component file you want to display as like lightning__AppPage, lightning__RecordPage, lightning__HomePage.

Final Output

get selected radio button value and checked default 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 ➑ lwcRadioGroup.html

SFDX:Lightning Web Component ➑ New ➑ lwcRadioGroup.html

lwcRadioGroup.html [Lightning Web Component HTML]

  1.    <template>
  2. <lightning-card title="How can I get radio button selected value in Lightning Web Component -- LWC" icon-name="custom:custom18" size="small">
  3.     <div class="slds-p-around_medium">
  4.         <div class="slds-grid slds-wrap">
  5.             <div class="slds-col slds-size_2-of-12">
  6.             <lightning-radio-group name="radioGroup"
  7.             label="Radio Group"
  8.             options={options}
  9.             value={value}
  10.             type="radio"
  11.             onchange={handleRadioChange}>
  12.  
  13.             </lightning-radio-group>
  14.             </div>
  15.  
  16.             <div class="slds-col slds-size_10-of-12">
  17.             <template if:true={blogFieldValue}>
  18.                 <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Blog</strong></h3> 
  19.                 <p>How to start a blog and make money. Easy Guide Step-By-Step for Beginners follow given points. What is blog and how to start your blog? Blog is a platform where you can share your personal experience that you have in expertise. <span class="readMore"><a href="https://www.w3web.net/blog/" target="_blank" rel="noopener noreferrer">more...</a></span></p>
  20.  
  21.             </template>  
  22.  
  23.             <template if:true={tutorialFieldValue}>
  24.                 <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Tutorial</strong></h3> 
  25.                 <p>An easy way to learn step-by-step online salesforce free tutorial from <strong>w3web.net</strong>, To learn <span class="readMore"><a href="https://www.w3web.net/tutorial/" target="_blank" rel="noopener noreferrer">more...</a></span></p> 
  26.             </template> 
  27.  
  28.             <template if:true={techGuideFieldValue}>
  29.                 <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Tech Guide</strong></h3> 
  30.                 <p>Many people writing a Blog or Article. There can be multiple pages in a single article, if you have not good hand typing speed you will be face so many difficulty. If you want to write a article English or Hindi very fast than go through the β€œRemote Mouse App”. <span class="readMore"><a href="https://www.w3web.net/technical-guide/" target="_blank" rel="noopener noreferrer">more...</a></span></p> 
  31.             </template> 
  32.  
  33.             <template if:true={auraCompFieldValue}>               
  34.                 <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Aura Component</strong></h3> 
  35.                 <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> 
  36.             </template> 
  37.  
  38.             <template if:true={salesforceLwcFieldValue}>               
  39.                 <h3 class="slds-text-heading_medium slds-text-color--error"><strong> Salesforce LWC</strong></h3> 
  40.                 <p>Enhance your Salesforce users’ experience with customizable Lightning Web Components. Find the Lightning Web Components that are right for you on AppExchange. Connect With Customers. Collaborate Better. Salesforce Admin Tools. To live demo project learn  <span class="readMore"><a href="https://www.w3web.net/tutorial/salesforce-lwc/" target="_blank" rel="noopener noreferrer">more...</a></span></p> 
  41.             </template> 
  42.             </div>
  43.         </div> 
  44.  
  45.  
  46.     </div>
  47.     <br/>
  48.  
  49.   <br/>
  50.    <!--Start RelatedTopics Section-->
  51. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  52.  
  53.             <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>
  54.  
  55.             <br/><br/>
  56.             <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>
  57.             <div style="display:block; overflow:hidden;"> 
  58.                 <div style="width: 50%; float:left; display:inline-block">
  59.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  60.                         <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>
  61.                         <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>
  62.                         <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>
  63.                         <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>
  64.                         <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>
  65.                     </ul>
  66.             </div>
  67.  
  68.             <div style="width: 50%; float:left; display:inline-block">
  69.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  70.                         <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>
  71.                         <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>
  72.                         <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>
  73.                         <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>
  74.                         <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>
  75.                     </ul>
  76.                 </div>
  77.                <div style="clear:both;"></div> 
  78.                <br/>
  79.                 <div class="youtubeIcon">
  80.                     <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>
  81.                 </div>
  82.     </div>
  83.  
  84. </div>
  85.  
  86.   <!--End RelatedTopics Section-->
  87.  
  88. </lightning-card>
  89.  
  90. </template>

Create Lightning Web Component Javascript

Step 2:- Create Lightning Web Component Javascript ➑ lwcRadioGroup.js

SFDX:Lightning Web Component ➑ New ➑ lwcRadioGroup.js

lwcRadioGroup.js [LWC JavaScript File]

  1.    import { LightningElement, track } from 'lwc';
  2.  
  3. export default class LwcRadioGroup extends LightningElement {
  4.     value = 'Blog';
  5.  
  6.     get options() {
  7.         return [
  8.             { label: 'Blog', value: 'Blog' },
  9.             { label: 'Tutorial', value: 'Tutorial' },
  10.             { label: 'Tech Guide', value: 'Tech Guide' },
  11.             { label: 'Aura Component', value: 'Aura Component' },
  12.             { label: 'Salesforce LWC', value: 'Salesforce LWC' },
  13.         ];
  14.     }
  15.  
  16.     @track blogFieldValue = true;
  17.     @track tutorialFieldValue = false;
  18.     @track techGuideFieldValue = false;
  19.     @track auraCompFieldValue = false;
  20.     @track salesforceLwcFieldValue = false;
  21.  
  22.     handleRadioChange(event) {
  23.         const selectedOption = event.detail.value;
  24.         //alert('selectedOption ' + selectedOption);
  25.         if (selectedOption == 'Blog'){
  26.             this.blogFieldValue = true;
  27.         }else{
  28.             this.blogFieldValue = false;
  29.         }
  30.  
  31.  
  32.         if (selectedOption == 'Tutorial'){
  33.             this.tutorialFieldValue = true;
  34.         }else{
  35.             this.tutorialFieldValue = false;
  36.         }
  37.  
  38.  
  39.         if (selectedOption == 'Tech Guide'){
  40.             this.techGuideFieldValue = true;
  41.         }else{
  42.             this.techGuideFieldValue = false;
  43.         }
  44.  
  45.  
  46.       if (selectedOption == 'Aura Component'){
  47.             this.auraCompFieldValue = true;
  48.         }
  49.         else{
  50.             this.auraCompFieldValue = false;
  51.         }
  52.  
  53.         if (selectedOption == 'Salesforce LWC'){
  54.             this.salesforceLwcFieldValue = true;
  55.         }
  56.         else{
  57.             this.salesforceLwcFieldValue = false;
  58.         }
  59.  
  60.  
  61.     }
  62.  
  63.  
  64. }

Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component Meta XML ➑ lwcRadioGroup.js-meta.xml

SFDX:Lightning Web Component ➑ New >> lwcRadioGroup.js-meta.xml

lwcRadioGroup.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 radio button value and checked default in lwc -- w3web.net

 

Further post that would you like to learn in Salesforce

 

 


 

FAQ (Frequently Asked Questions)

How do you make a radio group horizontal in LWC?

Use label-inline to horizontally align the label and radio group. Use label-stacked to place the label above the radio group.

How do I make radio buttons horizontal in HTML?

To make a horizontal radio button set, add the data-type='horizontal' to the fieldset .

Does salesforce have radio buttons?

Radio button is not a standard data type in salesforce, you either need to use checkboxes are picklists to replicate the same functionality.

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