Create horizontal tabs uses of selected radio group button using ‘lightning-radio-group’ element and display selected radio value in Salesforce Lightning Web Component — LWC | How to display selected radio value in lwc

1,950 views

Hey guys, today in this post we are going to learn about LWC how to Create horizontal tabs uses of selected radio group button value using ‘lightning-radio-group’ element and display selected radio value in Salesforce 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. To know more, click here..

To create radio buttons, pass in the following properties to the options attribute.

Why Should You Attend It?

🎯 If You Are Facing Any Of These 6 Challenges. This Masterclass Is For You.

  • 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?

 
PROPERTY TYPE DESCRIPTION
label string The text that displays next to a radio button.
value string The string that’s used to identify which radio button is selected.

The radio group is nested in a fieldset element that contains a legend element. The legend contains the label value. The fieldset element enables grouping of related radio buttons to facilitate tabbing navigation and speech navigation for accessibility purposes. Similarly, the legend element improves accessibility by enabling a caption to be assigned to the fieldset.

lightning-radio-group is available in the Base Components Recipes GitHub repository. It’s transpiled into the c namespace so that you can use it in your own projects.

Component Styling

lightning-radio-group implements the radio button blueprint in the Salesforce Lightning Design System (SLDS).

Set type=”button” to create a component that implements the radio button group blueprint in SLDS.

Variant attribute of radio group

  • label-hidden hides the radio group label but makes it available to assistive technology. This variant does not hide the option labels.
  • label-inline horizontally aligns the label and radio group.
  • label-stacked places the label above the radio group.
  • standard is the default variant, which displays the radio group label above the options.

 

 

Files we used in this post example

 

lwcRadioButtonGroup.html LWc HTML File Template HTML file to get selected radio group button value in Salesforce Lightning Web Component (LWC)
lwcRadioButtonGroup.js LWC JavaScript File It’s hold a javascript on handlechange function to get selected radio group button value and display in lightning component.
lwcRadioButtonGroup.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 value of radio is checked in lightning web component -- 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 ➑ lwcRadioButtonGroup.html

SFDX:Lightning Web Component ➑ New ➑ lwcRadioButtonGroup.html

lwcRadioButtonGroup.html [Lightning Web Component HTML]

  1.    <template>
  2.     <lightning-card title="Get Radio Group Button Selected Value in Lightning Web Component -- LWC" icon-name="custom:custom19" size="small"> 
  3.     <div class="slds-p-around_medium">
  4.                  <lightning-radio-group name="radioGroup"                        
  5.                         options={options}
  6.                         value={value}                        
  7.                         type="button" onchange={handleRadioChange}>
  8.                     </lightning-radio-group>
  9.  
  10.                     <section aria-label="Dialog title" aria-describedby="popover-body-id" class="slds-popover" style="width: 50%;">
  11.  
  12.                         <div  class="slds-popover__body">
  13.                             <div class="slds-grid slds-wrap">
  14.                                 <div class="slds-col slds-size_12-of-12" style="background: #eee; padding: 10px;">
  15.  
  16.                                             <template if:true={tutorialValue}>
  17.                                                 <p class="slds-text-heading_medium slds-text-color--error"><strong>Tutorial</strong></p> 
  18.                                                 <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> 
  19.                                             </template> 
  20.  
  21.                                             <template if:true={auraCompValue}>               
  22.                                                 <p class="slds-text-heading_medium slds-text-color--error"><strong>Aura Component</strong></p> 
  23.                                                 <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> 
  24.                                             </template> 
  25.  
  26.                                             <template if:true={salesforceLwcValue}>               
  27.                                                 <p class="slds-text-heading_medium slds-text-color--error"><strong> Salesforce LWC</strong></p> 
  28.                                                 <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> 
  29.                                             </template> 
  30.  
  31.                                         </div>
  32.                                     </div> 
  33.                         </div>
  34.                       </section>    
  35.  
  36.             </div>
  37.             <br/><br/>
  38.    <!--Start RelatedTopics Section-->
  39. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  40.  
  41.             <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>
  42.  
  43.             <br/><br/>
  44.             <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>
  45.             <div style="display:block; overflow:hidden;"> 
  46.                 <div style="width: 50%; float:left; display:inline-block">
  47.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  48.                         <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>
  49.                         <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>
  50.                         <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>
  51.                         <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>
  52.                         <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>
  53.                     </ul>
  54.             </div>
  55.  
  56.             <div style="width: 50%; float:left; display:inline-block">
  57.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  58.                         <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>
  59.                         <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>
  60.                         <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>
  61.                         <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>
  62.                         <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>
  63.                     </ul>
  64.                 </div>
  65.                <div style="clear:both;"></div> 
  66.                <br/>
  67.                 <div class="youtubeIcon">
  68.                     <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>
  69.                 </div>
  70.     </div>
  71.  
  72. </div>
  73.  
  74.   <!--End RelatedTopics Section-->
  75.  
  76.    </lightning-card> 
  77. </template>
  78.  
  79.         </lightning-card> 
  80. </template>

Create Lightning Web Component Javascript

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

SFDX:Lightning Web Component ➑ New ➑ lwcRadioButtonGroup.js

lwcRadioButtonGroup.js [LWC JavaScript File]

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

Create Lightning Web Component Meta XML

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

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

lwcRadioButtonGroup.js-meta.xml [LWC Meta Data XML]

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

Create Lightning Application

Step 4:- Create Lightning Application : lwcRadioButtonGroupApp.app

From Developer Console ➑ File ➑ New ➑ Lightning Application

lwcRadioButtonGroupApp.app [Component Application File]

  1.     <c:lwcRadioButtonGroup/>

 

 

 

Further post that would you like to learn in Salesforce

 

 

 

 

FAQ (Frequently Asked Questions)

What is button group in radio button?

The ButtonGroup component manages the selected/unselected state for a set of buttons.

How do you make a radio button group required?

To group radio buttons should have the same value for the name attribute. It's allows us to select only one radio button at once and apply the required attribute to the whole group.

Which attribute sets the current state of radio button?

checked attribute in radio button is used to set the current state of a radio button.

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