How to change the state of button value as ‘follow/unfollow’ or ‘like/dislike’ through onclick handler method uses of ‘lightning-button-stateful’ tag elements in Salesforce LWC (Lightning Web Component) | How to change the state of lightning:buttonStateful label value as ‘like/dislike’ or ‘following/unfollowing’ use an onclick handler method in Lightning Web Component – LWC (Salesforce)

2,652 views


Hey guys, today in this post we are going to learn about How to change the state of button value as ‘follow/unfollow’ or ‘like/dislike’ through onclick handler method uses of ‘lightning-button-stateful’ tag elements in Salesforce LWC (Lightning Web Component).

A lightning-button-stateful component represents a button that toggles between states, similar to a Like button on social media. Stateful buttons can show a different label and icon based on their selected states.

To handle the state change when the button is clicked, use the onclick event handler. This example enables you to toggle the button between states, displaying the “Follow” label by default, and replacing it with “Following” when the button is selected. Selecting the button toggles the selected state to true, and deselecting it toggles selected state to false. When the selected state is true, the button displays “Unfollow” when you mouse over it or when it receives focus. To know more details about lightning-button-stateful, Click Here.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

 

Files we used to create lightning-button-stateful as ‘follow/unfollow’ Or ‘like/dislike’ in Salesforce LWC →

lwcButtonStateful.html LWc HTML File Template HTML file to create lightning-button-stateful as ‘follow/unfollow’ Or ‘like/dislike’ in Salesforce Lightning Web Component (LWC)
lwcButtonStateful.js LWC JavaScript File It’s javascript actions functions to ‘like/dislike’ or toggle the ‘lightning-button-stateful’ in Salesforce LWC
lwcButtonStateful.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.
lwcButtonStatefulApp.app Lightning Application It is used for call the component and preview on browser.

 

 

Final Output →

change stateful button 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 : lwcButtonStateful.html

SFDX:Lightning Web Component >> New >> lwcButtonStateful.html

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

lwcButtonStateful.html [Lightning Web Component HTML]

  1.    <template>
  2.     <lightning-card>
  3.         <div class="slds-p-around--medium">
  4.         <h3 class="slds-text-heading_small"><lightning-icon icon-name="custom:custom94" size="small"></lightning-icon> <strong style="color:#270086; font-size:13px; margin-right:5px;"> Create Stateful Button with changes its text and icon again when you hover over it in Lightning Web Component (LWC) </strong></h3>
  5.         <br/><br/>
  6.         <table class="slds-table slds-table_cell-buffer slds-table_col-bordered slds-table_bordered" style="border:1px #ddd solid;">
  7.              <tbody>
  8.                 <tr>
  9.                     <td style="vertical-align: top;">
  10.                         <h2 class="header" style="color: #ff8000; margin-bottom:20px;"><strong>Stateful Button</strong> </h2>                   
  11.                         <lightning-button-stateful
  12.                         label-when-off="Follow"
  13.                         label-when-on="Following"
  14.                         label-when-hover="Unfollow"
  15.                         icon-name-when-off="utility:add"
  16.                         icon-name-when-on="utility:check"
  17.                         icon-name-when-hover="utility:close"
  18.                         selected={isSelected}
  19.                         onclick={handleClick}>
  20.                     </lightning-button-stateful>
  21.                     </td>
  22.                     <td style="vertical-align: top;">       
  23.                         <h2 class="header" style="color: #ff8000; margin-bottom:20px;"><strong>Stateful Button with Inverse Variar</strong> </h2>             
  24.                         <div class="backgroundInverse">
  25.                             <lightning-button-stateful
  26.                                 label-when-off="Follow"
  27.                                 label-when-on="Following"
  28.                                 label-when-hover="Unfollow"
  29.                                 icon-name-when-off="utility:add"
  30.                                 icon-name-when-on="utility:check"
  31.                                 icon-name-when-hover="utility:close"
  32.                                 variant="inverse"
  33.                                 selected={isSelectedInverse}
  34.                                 onclick={inverseHandleClick}>
  35.                             </lightning-button-stateful>
  36.                         </div>
  37.                     </td>
  38.                 </tr>
  39.  
  40.                 <tr>
  41.                     <td style="vertical-align: top;">
  42.                         <h2 class="header" style="color: #ff8000; margin-bottom:20px;"><strong>Stateful Button with No Icon</strong> </h2>                   
  43.                         <lightning-button-stateful
  44.                         label-when-off="Follow"
  45.                         label-when-on="Following"
  46.                         label-when-hover="Unfollow"
  47.                         variant="brand"
  48.                         selected={isSelectedNoIcon}
  49.                         onclick={noIconHandleClick}>
  50.                     </lightning-button-stateful>
  51.                     </td>
  52.                     <td style="vertical-align: top;">       
  53.                         <h2 class="header" style="color: #ff8000; margin-bottom:20px;"><strong>Stateful Button with Text and Icon</strong> </h2>             
  54.                         <div class="backgroundInverse">
  55.                             <lightning-button-stateful
  56.                                 label-when-off="Like"
  57.                                 label-when-on="Liked"
  58.                                 icon-name-when-off="utility:like"
  59.                                 icon-name-when-on="utility:like"
  60.                                 variant="text"
  61.                                 selected={isSelectedTextIcon}
  62.                                 onclick={textIconHandleClick}>
  63.                             </lightning-button-stateful>
  64.                         </div>
  65.                     </td>
  66.                 </tr>
  67.              </tbody>
  68.         </table>
  69.  
  70.         <br/><br/>
  71.         <!--Start RelatedTopics Section-->
  72. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  73.  
  74.             <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>
  75.  
  76.             <br/><br/>
  77.             <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>
  78.             <div style="display:block; overflow:hidden;"> 
  79.                 <div style="width: 50%; float:left; display:inline-block">
  80.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  81.                         <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>
  82.                         <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>
  83.                         <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>
  84.                         <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>
  85.                         <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>
  86.                     </ul>
  87.             </div>
  88.  
  89.             <div style="width: 50%; float:left; display:inline-block">
  90.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  91.                         <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>
  92.                         <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>
  93.                         <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>
  94.                         <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>
  95.                         <li><a href="https://www.w3web.net/tree-grid-dynamic-expand-collapse-in-lwc/" target="_blank" rel="noopener noreferrer">how to create tree grid with expanded/collapsed section for the entire row in LWC</a></li>
  96.                     </ul>
  97.                 </div>
  98.                <div style="clear:both;"></div> 
  99.                <br/>
  100.                 <div class="youtubeIcon">
  101.                     <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>
  102.                 </div>
  103.     </div>
  104.  
  105. </div>
  106.  
  107.   <!--End RelatedTopics Section-->
  108. </div>
  109.     </lightning-card>
  110. </template>

 

Create Lightning Web Component JavaScript →

Step 2:- Create Lightning Web Component : lwcButtonStateful.js

SFDX:Lightning Web Component >> New >> lwcButtonStateful.js

lwcButtonStateful.js [LWC JavaScript File]

  1.   import { LightningElement } from 'lwc';
  2.  
  3. export default class LwcButtonStateful extends LightningElement {
  4.     isSelected = false;
  5.     isSelectedInverse = false;
  6.     isSelectedNoIcon = false;
  7.     isSelectedTextIcon = false;
  8.  
  9.     handleClick() {
  10.         this.isSelected = !this.isSelected;
  11.     }
  12.     inverseHandleClick() {
  13.         this.isSelectedInverse = !this.isSelectedInverse;
  14.     }
  15.     noIconHandleClick() {
  16.         this.isSelectedNoIcon = !this.isSelectedNoIcon;
  17.     }
  18.     textIconHandleClick() {
  19.         this.isSelectedTextIcon = !this.isSelectedTextIcon;
  20.     }
  21. }

 

Create Lightning Web Component Meta XML →

Step 3:- Create Lightning Web Component : lwcButtonStateful.js-meta.xml

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

lwcButtonStateful.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>

 

Create Lightning Application →

Step 4:- Create Lightning Application : lwcButtonStatefulApp.app

From Developer Console >> File >> New >> Lightning Application

lwcButtonStatefulApp.app [Component Application File]

  1.    <aura:application extends="force:slds">
  2.    <c:lwcButtonStateful/>
  3. </aura:application>

 
change stateful button value in lwc -- w3web.net
 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

What is stateful button?

The stateful button is a button used to display an actionable icon. The stateful button doesn't share all the properties of the standard button component. The stateful button only displays an icon and cannot display any other elements, like labels. The button can be toggled between an on and off state.

How do you make a responsive lightning button?

The first step in creating a responsive layout is to implement a fluid grid. The SLDS grid utility provides a mobile-first layout system with granular column control. To implement the grid system in a Lightning web component, use the lightning-layout and lightning-layout-item base components.

How do you disable the Lightning button after clicking?

For Disabling a button you can create an boolean attribute in the component and use this attribute in the disabled attribute of button tag. Whenever you want to disable the button in the click you can make the attribute to true,so that the button will be disabled.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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