Solved
Pop-up alert on form, on selecting a field value
Is there a way to render a pop-up alert to the user, when user selects a particular value from the drop-down field on the form before submitting?
Thank you!
Is there a way to render a pop-up alert to the user, when user selects a particular value from the drop-down field on the form before submitting?
Thank you!
MktoForms2.whenReady(function(form){
var interestingSelectionsByField = [
{
name : "Country",
values : ["US"]
}
];
var interestingSelectionsMsg = "You made an interesting selection.";
/* --- NO NEED TO TOUCH BELOW THIS LINE! --- */
var formEl = form.getFormElem()[0];
MktoForms2.whenRendered(function(form){
interestingSelectionsByField
.map(function(fieldDesc){
return formEl.querySelector("[name='" + fieldDesc.name + "']");
})
.forEach(function(fieldEl){
fieldEl.addEventListener("change", alertOnInterestingSelection);
});
});
function alertOnInterestingSelection(e){
var currentFormValues = form.getValues(),
currentFieldName = this.name;
var currentInterestingSelections = interestingSelectionsByField
.filter(function(fieldDesc){
return currentFieldName == fieldDesc.name ;
})[0];
var isInterestingSelection = currentInterestingSelections.values
.some(function(value){
return currentFormValues[currentFieldName] == value;
});
if(isInterestingSelection){
window.alert(interestingSelectionsMsg);
}
}
});
No framework dependencies, supports multiple fields & interesting values, handles Visibility Rules.
N.B. It's not awesome to have to use DOM events here instead of Forms API events, but there's no choice.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.