Reposition Fields in a Marketo Form | Community
Skip to main content
Kenny_Elkington
Level 10
October 30, 2015

Reposition Fields in a Marketo Form

While the Marketo form builder is quite powerful, there are some edge cases where it doesn't produce quite the result that you're looking for.  A common request is to reposition fields after the progressive profiling box, which is a capability that isn't currently available natively.  Fortunately, with some javascript tweaks, we can reposition fields freely.  Let's take a look at the code:

/*

jQuery must be loaded on the page

use moveMktoField to position the field designated by FieldName relative to the field designated by pivotName

*/

function moveMktoField(fieldName, pivotName, beforeOrAfter){

  //find the label element for the field which we want to move

  var labelForFieldToMove = $('[for="' + fieldName + '"]');

  //find the label element for the field we want to position relative to

  var labelForPivot = $('[for="' + pivotName + '"]');

  //get the mktoFormRow parent of each

  var fieldToMove = getParentWithClass(labelForFieldToMove, "mktoFormRow");

  var pivot = getParentWithClass(labelForPivot, "mktoFormRow");

  //insert the field before or after the pivot based on the setting

  if (beforeOrAfter == "before"){

       fieldToMove.insertBefore(pivot);

  } else if (beforeOrAfter == "after"){

       fieldToMove.insertAfter(pivot);

  } else {

       console.log("argument 'beforeOrAfter' must be one of 'before' or 'after'");

       return null;

  }

}

//inserts multiple fields in order where the first in the fields array is adjacent to the pivot, and the last is furthest

function moveMktoFields(fields, pivotName, beforeOrAfter){

  moveMktoField(fields[0], pivotName, beforeOrAfter);

  for ( i = 1; i < array.length; i++ ){

       moveMktoField(fields[i], fields[i - 1], beforeOrAfter);

  }

}

function getParentWithClass(elem, withClass) {

  //Check the parent to see if it has the desired class

  if ( elem.parent().hasClass(withClass) ) {

       return elem.parent();

  } else {

       //if not, call self recursively on the immediate parent

       return (getParentWithClass(elem.parent(), withClass) );

  }

}

With the above, we can use the moveMktoField function to reposition one field relative to another.  You can also use moveMktoFields to move the fields as a group when you pass an array of field names to it.  Click here to see a live demo, moving First Name after Last Name.​

43 replies

January 28, 2016

Forgive a newbie question but where do I put that code?  I'm not a developer but am comfortable with copy/paste and rearranging the code. Thanks!

Translate
December 17, 2015

Thanks again for the fast reply. I removed the setTimeout function, but then the Javascript wasn't executed.

My workaround now is to set the time from 2000 to 1, so it's reordering the fileds nearly instant after loading.

That fits my customers needs in the best way because they wanted a fast solution.

Translate
Kenny_Elkington
Level 10
December 16, 2015

You've kept the setTimeout included, which is causing the 2 second delay.  This is in my demo script just to show it in action so you can see the difference between pre and post-execution.  Just remove the setTimeout and you should be good:

This:

MktoForms2.whenReady(function(form){

  moveMktoField("FirstName", "LastName", "after");

});

Instead of this:

MktoForms2.whenReady(function(form){

setTimeout(function(){

  moveMktoField("FirstName", "LastName", "after");

}, 2000);

});

You also only need to call whenReady once, so both of your calls to moveMktoField can be inside of that single callback.

Translate
December 16, 2015

Thank you for your Script, Kenny.

I'm not a Java Script designer but I was able to adapt it for my progressive profiling form.

The only problem is, that the script loads not fast enough.The first seconds the fields in the form are shown in the wrong order before the script is loading and repositions them as required. Do you have any tips how the loading time can be improved or reduced to at least under one second?

Example wit Progressive Profiling (Job Title and Postcode are the first two fields of the progressive profiling area). The script is located at the bottom of the source code.

Order should be:

1. All Time visible fields

2. Progressive Profiling

3. Newsletter Checkbox (with privacy statement)

eu-lon04.marketo.com/lp/824-MSG-799/Fields-Reorder-Test.html

Thanks for any comments

Stephan

Translate
Kenny_Elkington
Level 10
November 5, 2015

I'd like to help, but I'm not much of a designer, and this would take real HTML/CSS expertise to make it work right from a visual standpoint.  There's significantly more gotchas than reordering list elements there.

Translate
Level 8
November 5, 2015

@Kenny Elkington​ Can you help me with something?  How do I get progressive profiling fields to sit next to each other??  I want to do a form with 2 columns of fields, but can't do this whilst using progressive profiling.

Thanks

Juli

Translate
Kenny_Elkington
Level 10
November 2, 2015

I'm not really sure what you're asking about here, Christine.  Could you clarify this?  Are you trying to profile subsequent forms?

Translate
October 31, 2015

Can this help you show no progressive profiling fields on the first view, and only show progressive profiling fields in future views? I'd like to show only the regular required fields the first time, ie first, last, name, and email. But going forward show progressive profiling fields two at a time.

Translate
Level 8
October 30, 2015

AMAZING!  I'll be using this from now on.

Thanks

Translate
Kenny_Elkington
Level 10
October 30, 2015

Yep, that's what it's for.

Translate