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.
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
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]
<template>
<lightning-card title="How can I get radio button selected value in Lightning Web Component -- LWC" icon-name="custom:custom18" size="small">
<div class="slds-p-around_medium">
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_2-of-12">
<lightning-radio-group name="radioGroup"
label="Radio Group"
options={options}
value={value}
type="radio"
onchange={handleRadioChange}>
</lightning-radio-group>
</div>
<div class="slds-col slds-size_10-of-12">
<template if:true={blogFieldValue}>
<h3 class="slds-text-heading_medium slds-text-color--error"><strong>Blog</strong></h3>
<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>
</template>
<template if:true={tutorialFieldValue}>
<h3 class="slds-text-heading_medium slds-text-color--error"><strong>Tutorial</strong></h3>
<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>
</template>
<template if:true={techGuideFieldValue}>
<h3 class="slds-text-heading_medium slds-text-color--error"><strong>Tech Guide</strong></h3>
<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>
</template>
<template if:true={auraCompFieldValue}>
<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>
<template if:true={salesforceLwcFieldValue}>
<h3 class="slds-text-heading_medium slds-text-color--error"><strong> Salesforce LWC</strong></h3>
<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>
</template>
</div>
</div>
</div>
<br/>
<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>
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]
import { LightningElement, track } from 'lwc';
export default class LwcRadioGroup extends LightningElement {
value = 'Blog';
get options() {
return [
{ label: 'Blog', value: 'Blog' },
{ label: 'Tutorial', value: 'Tutorial' },
{ label: 'Tech Guide', value: 'Tech Guide' },
{ label: 'Aura Component', value: 'Aura Component' },
{ label: 'Salesforce LWC', value: 'Salesforce LWC' },
];
}
@track blogFieldValue = true;
@track tutorialFieldValue = false;
@track techGuideFieldValue = false;
@track auraCompFieldValue = false;
@track salesforceLwcFieldValue = false;
handleRadioChange(event) {
const selectedOption = event.detail.value;
//alert('selectedOption ' + selectedOption);
if (selectedOption == 'Blog'){
this.blogFieldValue = true;
}else{
this.blogFieldValue = false;
}
if (selectedOption == 'Tutorial'){
this.tutorialFieldValue = true;
}else{
this.tutorialFieldValue = false;
}
if (selectedOption == 'Tech Guide'){
this.techGuideFieldValue = true;
}else{
this.techGuideFieldValue = false;
}
if (selectedOption == 'Aura Component'){
this.auraCompFieldValue = true;
}
else{
this.auraCompFieldValue = false;
}
if (selectedOption == 'Salesforce LWC'){
this.salesforceLwcFieldValue = true;
}
else{
this.salesforceLwcFieldValue = false;
}
}
}
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]
<?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
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 |