Is there a way to add $A.util.addClass() on multiple elements all at once, or do I always need to declare it for each individual element? For the elements below I want to add or remove a class, is there a way to chain all addClass() together?

$A.util.addClass(lowercaseDiv, 'slds-hide');
$A.util.addClass(upparcaseDiv, 'slds-hide');
$A.util.addClass(numberDiv, 'slds-hide');
$A.util.addClass(specialCharDiv, 'slds-hide');
$A.util.addClass(minCharDiv, 'slds-hide');
$A.util.removeClass(lowercaseGap, 'slds-hide');
$A.util.removeClass(upparcaseGap, 'slds-hide');
$A.util.removeClass(numberGap, 'slds-hide');
$A.util.removeClass(specialCharGap, 'slds-hide');
$A.util.removeClass(minCharGap, 'slds-hide');

There is no standard aura javascript APIs for that. However, you can use below helper function:

HELPER method:

processClass : function(elements, process, eclass) {
        elements = [elements];
        elements.forEach(elem=>$A.util.addClass(elem, eclass));
    else if(process==='remove')
        elements.forEach(elem=>$A.util.removeClass(elem, eclass));
    else if(process==='toggle')
        elements.forEach(elem=>$A.util.toggleClass(elem, eclass));

You can use this as below:

let div1 = component.find('div1'),
    div2 = component.find('div2');

In your use case, you use below:

helper.processClass([lowercaseDiv, upparcaseDiv],'add','slds-hide');
// helper.processClass([lowercaseDiv, upparcaseDiv],'remove','slds-hide');
['lowercaseDiv','upparcaseDiv','upparcaseDiv','specialCharDiv','minCharDiv'].forEach(function(cmp) => {
 $A.util.addClass(cmp, 'slds-hide');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.