Question
Unchecking all other fields when 'Unsubscribe' checkbox is selected
Hello all,
I am trying to follow previous posts regarding this topic, and the code that I am testing isn't working correctly. I am attempting to add this code to the header section on the landing page that the form is contained on:
MktoForms2.whenReady(function(mktoForm){
const formEl = mktoForm.getFormElem()[0];
const unsubscribeTrigger = "Unsubscribed",
unsubscribableDeps = [
"personalBankingOptIn",
"commercialBankingOptIn",
"privateBankingOptIn",
"homeOptIn",
"economicPerspectivesOptIn",
"economicUpdateOptIn",
"weekinReviewOptIn",
"quarterlyInvestmentNewsletterOptIn",
"quarterlyWebConferenceOptIn"
];
const dependencies = [
{
primaries : unsubscribeTrigger,
cascadeSecondaries : unsubscribableDeps
.map(function(unsubscribable){
return { field : unsubscribable, negate : true, filterPrimaryValues : ["yes"] }
})
},
{
primaries : unsubscribableDeps,
cascadeSecondaries : { field : unsubscribeTrigger, negate : true, filterPrimaryValues : ["yes"] }
}
];
const mktoNegations = {
"yes" : "no",
"no" : "yes"
};
dependencies.forEach(function(dep){
if(!Array.isArray(dep.primaries)){
dep.primaries = [dep.primaries];
}
dep.primaries.forEach(function(primary){
const primaryEl = formEl.querySelector("[name='" + primary + "']");
if(primaryEl){
primaryEl.addEventListener("click",function(){
const currentValues = mktoForm.getValues(),
primaryValue = currentValues[primary];
if(!Array.isArray(dep.cascadeSecondaries)){
dep.cascadeSecondaries = [dep.cascadeSecondaries];
}
const mktoFormsObj = dep.cascadeSecondaries
.filter(function isMatchedPrimary(fieldDesc){
return fieldDesc.filterPrimaryValues.indexOf(primaryValue) != -1;
})
.filter(function isOnForm(fieldDesc){
return fieldDesc.field in currentValues;
})
.reduce(function assembleFinal(acc,nextField){
acc[nextField.field] = nextField.negate ? mktoNegations[primaryValue] : primaryValue;
return acc;
},{});
mktoForm.setValues(mktoFormsObj);
});
}
});
});
});
The landing page is: https://pages.watrust.com/2025-Preference-Center_DRAFT-Preference-Center---Manage-Preferences.html
Can anyone give me any insight on where I'm going wrong? Thank you!