How can I change the "Industry" when the visitor chooses "Teacher"? This should write "Education" in there?
Solved! Go to Solution.
MktoForms2.whenReady(function(form){
form.onSubmit(function(form){
var el = document.getElementById("teklaLeadType");
var sel = el.options[el.selectedIndex].innerHTML;
switch (sel) {
case 'Teacher':
form.addHiddenFields({Industry:'Education'});
break;
case 'Some other type':
form.addHiddenFields({Industry:'Some other industry'});
break;
default:
}
});
})
MktoForms2.whenReady(function(form){
form.onSubmit(function(form){
switch (form.getValues()['Lead_Type']) {
case 'Teacher':
form.addHiddenFields({Industry:'Education'});
break;
case 'Some other type':
form.addHiddenFields({Industry:'Some other industry});
break;
default:
}
});
});
Obviously you need to use the exact names of your fields, which you can see by inspecting the form's HTML or looking in Field Management.
I cried victory too quick.
It seems that the form.getValues()['Lead_Type'] takes the values and not the labels. If the label is teacher, the getvalues take "Lead" as value.
MktoForms2.whenReady(function(form){
form.onSubmit(function(form){
var el = document.getElementById("teklaLeadType");
var sel = el.options[el.selectedIndex].innerHTML;
switch (sel) {
case 'Teacher':
form.addHiddenFields({Industry:'Education'});
break;
case 'Some other type':
form.addHiddenFields({Industry:'Some other industry'});
break;
default:
}
});
})
Yes, of course it takes the values.
I wouldn't recommend this approach because it's fragile to make values depend on cosmetic labels. Instead, have the Industry and Lead Type both encoded in the value:
Student | Education.Student
Teacher | Education.Lead
Other | Other.Lead
Then destructure and reassign:
var leadInfo = form.getValues()['Lead_Type'].split('.');
form.setValues({
Industry: leadInfo[0],
Lead_Type: leadInfo[1]
});