SOLVED

Set default values directly on embedded form

Go to solution
skiley
Level 1

Set default values directly on embedded form

Hi all. I currently have one global Marketo form that I embed on all our Unbounce landing pages and pass-through values to hidden fields through URL parameters. This has worked great - unless someone sends out the landing page without the correct URL params. Does anyone know if there's a way to set default values directly in my code (and only if there are no URL params passed through)?

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Set default values directly on embedded form

You’d use the Marketo Forms API’s setValues() method:

MktoForms2.whenReady(function(mktoForm){
  const defaultValues = {
    hiddenField1 : "default value 1",
    hiddenField2: "default value 2"
  };

  let currentValues = mktoForm.getValues();

  let finalValues = Object.keys(defaultValues)
    .reduce(function(acc,key){
       if( !currentValues[key] ) {
         acc[key] = defaultValues[key];
       }
       return acc;
    },{});

  mktoForm.setValues(finalValues);        
});

 

View solution in original post

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Set default values directly on embedded form

You’d use the Marketo Forms API’s setValues() method:

MktoForms2.whenReady(function(mktoForm){
  const defaultValues = {
    hiddenField1 : "default value 1",
    hiddenField2: "default value 2"
  };

  let currentValues = mktoForm.getValues();

  let finalValues = Object.keys(defaultValues)
    .reduce(function(acc,key){
       if( !currentValues[key] ) {
         acc[key] = defaultValues[key];
       }
       return acc;
    },{});

  mktoForm.setValues(finalValues);        
});

 

skiley
Level 1

Re: Set default values directly on embedded form

Thanks, @SanfordWhiteman!