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

March 7, 2016

Hey Christina - you're almost there. You can use the code as-is from my original post, but all that does is define the function. You also need to call the function, and essentially just feed the names of your field(s) into the function. You'll need to go find the API name for Global Opt In (probably something like globalOptIn - you can find this in your field management settings). So, in your code, you will both define the function:

function moveMktoFieldsToBottom(fieldNames) {

  for ( i = 0; i < fieldNames.length; i++ ){

    var elementToMove = $('[for="' + fieldNames[i] + '"]').parents(".mktoFormRow");

    try {

      elementToMove.insertBefore(".mktoButtonRow");

    } catch(err) {

      console.log("Error: " + err.message);

    }

  }

}

And then you need to call it like this:

MktoForms2.whenReady(function(form){

    moveMktoFieldsToBottom(['globalOptIn']);

});

The way the code works is that you can tell it a list of the field names (should you have several) that you want to move to the bottom. In Javascript, lists are contained within brackets - this example doesn't look much like a list because there's only one value in it. But say you also wanted to move the email to the bottom for some reason, the function call would be something like:

MktoForms2.whenReady(function(form){

   moveMktoFieldsToBottom(['globalOptIn','email']);

});

The function will be triggered when the form is "ready" (that's the whenReady stuff), then cycles through each of these values and moves it to the bottom of the form, right above the button row.

Make sure that you have included the forms 2.0 javascript code before that function is called, or else the browser will not know what MktoForms2 means:

<script src="//app-ab11.marketo.com/js/forms2/js/forms2.min.js"></script>

Hope that helps!

Translate
March 7, 2016

Hi Ryan - I'm trying to use your code since it's the shortest in this thread and I'm not technical. At all.

I want to move my "global opt in" button to the bottom of my progressive profiling. The field name is "Global Opt In" so I placed it into the code (see italicized areas). However, there are other items like fieldname.length and .mktoformrow that I'm not sure about - do I need to put something into this? If so, where do I find that info?

function moveMktoFieldsToBottom(Global Opt In) {

  for ( i = 0; i < fieldNames.length; i++ ){

    var elementToMove = $('[for="' + Global Opt In[i] + '"]').parents(".mktoFormRow");

    try {

      elementToMove.insertBefore(".mktoButtonRow");

    } catch(err) {

      console.log("Error: " + err.message);

    }

  }

}

Translate
Kenny_Elkington
Level 10
February 16, 2016

Those links don't go anywhere right now.  You might be calling with a wrong field name since that error indicates that the call stack size is being exceeded by having too many things on the stack.  getParentWithClass is recursive so this could happen.  Should probably add a base case to avoid this.

Translate
February 16, 2016

This is an excellent JS that addresses exactly what I needed to do.  However, I'm having difficulty getting it to work when the setTimeout function is removed.  Here are 2 samples I made.  The goal is to bring the "Opt-In" checkbox to be positioned after the progressive profiling fields.

This page has a setTimeout set to 1000

www2.lucidas.co.jp/rearrange_test_timed.html

...whereas this one has no setTimeout, as shown in your code snippet.
www2.lucidas.co.jp/rearrange_test_nowait.html


When I try to run the second one in order to get instant results, it won't work, and my debugger says "RangeError: Maximum call stack size exceeded."
Knowing me, I probably am missing something stupid like a closing bracket or a double quote, but for the life of me it looks legit, although I'm wondering if trying to jump multiple fields rather than one may have some implications?

Translate
February 10, 2016

Here's a solution that allows you to move any elements to the bottom of the form, beneath any progressive form fields, and just above the button.

(Sorry, I don't know how to post code formatted as code here, like Kenny did in the initial post)

function moveMktoFieldsToBottom(fieldNames) {

  for ( i = 0; i < fieldNames.length; i++ ){

    var elementToMove = $('[for="' + fieldNames[i] + '"]').parents(".mktoFormRow");

    try {

      elementToMove.insertBefore(".mktoButtonRow");

    } catch(err) {

      console.log("Error: " + err.message);

    } 

  }

}

And here's a version of the moveMktoField from above that's a little cleaner:

function moveMktoField(fieldName, pivotName, beforeOrAfter){

  // set the default

  beforeOrAfter = beforeOrAfter || 'after';

  var elementToMove = $('[for="' + fieldName + '"]').parents('.mktoFormRow');

  var pivotElement = $('[for="' + pivotName + '"]').parents('.mktoFormRow');

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

  if (beforeOrAfter == "before"){

      elementToMove.insertBefore(pivotElement);

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

      elementToMove.insertAfter(pivotElement);

  }

}

Translate
Kenny_Elkington
Level 10
February 9, 2016

This solution doesn't currently account for it.  You'll need to provide a list of fields which can be profiled and then determine which are present and which you want to pivot on.

Translate
February 9, 2016

Hey Kenny Elkington​, thanks for this nice solution. As the general use case of this is to place the checkboxes after progressive fields at the base of a form, how are you assigning the pivotName value, when you don't know which particular progressive field is going to be showing up for a user?

If this isn't possible with the current code I will try and post a solution here.

Translate
Kenny_Elkington
Level 10
January 29, 2016

Hi Jennifer,

It is probably rendering as text since you do not have script tags wrapping it.  You would add this inside of an HTML block on your LP like so:

<script>

//code goes here

</script>

You could also do the same in a template.

Translate
January 29, 2016

Could you give more detail on what you did?  I'm not sure where to put it in a blank template.  So far, it just shows up as text, which is not what I want.

Translate
January 29, 2016

I added the code at the bottom of the Landingpage Template (used the Guided Editor). This works best for me. Hope the Developers will allow reordering of all form fields even under the Progressive Profiling part in the near future. Our Marketing Crowd needs an easy drag and drop solution here fast.

Translate