1

in this code, based on value from drop down i am tryig to change some css, it is removing css but while selecting another value from dropdown it is not adding css again.

handlePaymentTypeChange(event){
    info("Entering ac_LocatePaymentFlow.handlePaymentTypeChange()");
    this.displayRelatedSection = true; // display related fields on UI
    this.displayErrorBlock = false; 
    this.disableLocatePaymentBtn = false;
    this.selectedValue = event.detail.value;
    this.selectedPaymentType =event.detail.value;
    this.paymentTypeChange = true;

    if(this.template.querySelector('lightning-input')){
        var inputElements = this.template.querySelectorAll('lightning-input');
        inputElements.forEach(function(element){
            element.value = '';
            element.setCustomValidity('');
            element.reportValidity('');
        },this);
    }

    this.paymentType = event.detail.value;
    info("ac_LocatePaymentFlow.handlePaymentTypeChange().this.paymentType"+this.paymentType);
    this.template.querySelector('lightning-combobox').setCustomValidity('');
    this.template.querySelector('lightning-combobox').reportValidity();
    this.displayWorkCentre = false;
    this.displayCRN = false;
    this.displaytrn = false; 
    this.displayReceiptNumber = false;
    this.showPRNALL = true;
    this.showPRNAPAY = false;

    if (this.selectedPaymentType === this.labels.BPAY){
        this.labels.enterTransIds = 'Enter one transaction ID'
    } else {
        this.labels.enterTransIds = this.labels.originalEnterTransIds
    }    

    if (this.paymentType == this.labels.PostBillpay) {
        this.showPRNAPAY = true;
        this.showPRNALL = false;
        this.displayWorkCentre = false;
        this.displayReceiptNumber = false;
        this.islabelrequired = false;
        this.handleDynamicClassRemoval();
    } else if (this.paymentType == this.labels.Centrepay) {
        this.displayCRN = true;
        this.handleDynamicClassadd();
    } else if (this.paymentType == this.labels.BPAY) {
        info("IS BOH Agent"+this.isBOHAgent);
        this.handleDynamicClassadd();
        if(this.isBOHAgent){
             this.displaytrn = true;
        }else{
            this.displayErrorBlock = true;
            this.disableLocatePaymentBtn = true;
        }
    }
}

handleDynamicClassRemoval() {
    if (this.selectedPaymentType === this.labels.PostBillpay) {
        setTimeout(() => {
            const elementsToRemoveClass = this.template.querySelectorAll('.slds-m-top_medium.slds-border_top.slds-border_right.slds-border_bottom.slds-border_left');
            elementsToRemoveClass.forEach(element => {
                element.classList.remove('slds-m-top_medium', 'slds-border_top', 'slds-border_right', 'slds-border_bottom', 'slds-border_left');
            });
        });
    }
}

handleDynamicClassadd() { 
    if (this.selectedPaymentType === this.labels.Centrepay || this.selectedPaymentType === this.labels.BPAY) {
        setTimeout(() => {
            const elementsToRemoveClass = this.template.querySelectorAll('.slds-m-top_medium.slds-border_top.slds-border_right.slds-border_bottom.slds-border_left');
            elementsToRemoveClass.forEach(element => {
                element.classList.add('slds-m-top_medium', 'slds-border_top', 'slds-border_right', 'slds-border_bottom', 'slds-border_left');
            });
        });
    }
}
1
  • querySelectorAll doesn't return an iterable Array; you first need to convert it with an appropriate method, such as [...this.template.querySelectorAll(cssQuery)]. I guess there's also a possible race condition with rendering, given you're using setTimeout. It would be recommended to just use a getter and set the class list directly in the template, rather than this tedious logic. Commented Apr 2, 2024 at 7:55

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.