Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to change a lightning button variant which is in for loop?

Asked by Aswini Lobo May 11, 2023 1.2K views 1 answer
Share

About this question

HTML:

JS:
import { LightningElement,wire,api } from 'lwc';
export default class accData extends LightningElement {

accdata;

  @wire(getData, {accID: "$recordId" })
    accData({ error, data }) {
    if (error) {
        this.error = error;
    } else if (data) {
         this.accdata= data.map(item => {                
            return{
                data: item,
                buttonVariant: ''
            }
        }); 
    }
   handleClick(event){
        let dataSize = this.accdata.length;
        this.clickedRec =event.target.value;
        let objIndx = this.accdata.findIndex((item => item.data.Id === this.clickedRec));
        this.accdata[objIndx].buttonVariant='Brand';
        for (let i=0; i            if(i != objIndx){
                this.accdata[i].buttonVariant='neutral';
            }
        }       
    }
 }

when I click the button, since it's in the loop it's changing the variant for all the buttons that are in the loop, instead how can I just change the variant of the selected button in the loop.

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.