SOLVED

How can I change multiple field values with only one select?

Go to solution
Nick_Deboo1
Level 2

How can I change multiple field values with only one select?

Form
  • First name
  • Last name
  • Email
  • Country
  • I'm a (which is a field called "Lead type")
    • Student | Student
    • Teacher | Lead
    • Other | Lead

How can I change the "Industry" when the visitor chooses "Teacher"? This should write "Education" in there?

1 ACCEPTED SOLUTION

Accepted Solutions
Nick_Deboo1
Level 2

Re: How can I change multiple field values with only one select?

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:

    }

  });

})

View solution in original post

4 REPLIES 4
SanfordWhiteman
Level 10 - Community Moderator

Re: How can I change multiple field values with only one select?

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.

Nick_Deboo1
Level 2

Re: How can I change multiple field values with only one select?

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.

Nick_Deboo1
Level 2

Re: How can I change multiple field values with only one select?

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:

    }

  });

})

SanfordWhiteman
Level 10 - Community Moderator

Re: How can I change multiple field values with only one select?

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]

});