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

SanfordWhiteman
Level 10
June 13, 2018
I'm trying out your code

Which code?  1.0.x or 2.0.x branch?

Is the JS code placed on the form itself, on the template or as a HTML snippet on the page?

Forms JS always goes in a <script> tag, and you need to make sure it loads after the <script> that loads forms2.min.js. 

On a Marketo LP, that means you place your custom <script> just before the closing </body> tag.  On a non-Marketo LP put it right after the standard form embed code.

I'm guessing that the list order is what goes below the Submit Button. What if I need the field to go above a submit button but below a progressive profiling field?

You don't need to involve the Submit button at all in the order. The list ["Company","FirstName","Website"] will make sure those fields always appear in that order -- even if "FirstName" is a ProgPro field and "Website" is a regular (always-on) field, they'll appear in the stated order.

Translate
June 13, 2018

Hi Sanford,

I'm trying out your code and I'm having trouble getting it to work. Is the JS code placed on the form itself, on the template or as a HTML snippet on the page?

In this section:

var fieldOrder = [

submitButton,

"FirstName",

"Email",

"Website",

"ConsentDisclaimer"

];

I'm guessing that the list order is what goes below the Submit Button. What if I need the field to go above a submit button but below a progressive profiling field?

Any advice will be appreciated

cheers,

Marilyn

Translate
SanfordWhiteman
Level 10
May 29, 2018

Rajesh, you will likely find this code more flexible:

MktoForms2 :: Demo :: Override Field Order v2.0.2 - via Flexbox

If you need IE 8-9 compatibility you need the older, more complex approach:

MktoForms2 :: Demo :: Override Field Order 1.0.2

Translate
Level 2
May 29, 2018

Hi Ryan,

My name is Rajesh. I tried implementing your code it worked in edit mode of landing page, but when i see it in browser, the field does not moves.

What can be the reason. Here is my code for your reference.

<script>

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);

    }

  }

}

</script>

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

<script>

MktoForms2.whenReady(function(form){

    moveMktoFieldsToBottom(['Optin']);

});

</script>

Translate
SanfordWhiteman
Level 10
September 8, 2017

Wouldn't bother with this method. Use my code at MktoForms2 :: Override Field Order . Open a new thread if you have questions.

Translate
Kelly_Sheetz
Level 2
September 8, 2017

Hi Ryan,

I am struggling to get this to work. Here is my from: Test Form​

I want to move the field "ZOpComplianOptin" to the bottom. I do have a visibility rule set for when country = canada.

Here is my code:

<script src=“https://s3-us-west-2.amazonaws.com/aws-marketo-m2-resources/js/mkto-field-placement-pp/FieldPlacementPP2.js”</script> <script src="s3-us-west-2.amazonaws.com/aws-marketo-m2-resources/js/mkto-field-placement-pp/Marketoforms2.js"></script> <script> MktoForms2.whenReady(function(form){     moveMktoFieldsToBottom(['zOPComplianceOptin']); }); </script>

Thanks!

Translate
March 7, 2016

This line tells the function what to move:

moveMktoFieldsToBottom(['globalOptIn']);

Translate
March 7, 2016

It's magic! I guess I'm confused because there's nothing in this code that says "take this specific field and move it", how does it know I wanted Global Opt in to move instead of first name?

Translate
March 7, 2016

You've modified the original function - just use it as-is from my post. Note the differences in bold. Here's yours:

function moveMktoFieldsToBottom(globalOptIn) {

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

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

    try {

      elementToMove.insertBefore(".mktoButtonRow");

    } catch(err) {

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

    }

  }

}

Here's mine:

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);

    }

  }

}

Translate
March 7, 2016

Huh, I must be doing something wrong. I put this code exactly as is below into my LP on top of my footer (by adding an HTML element and dragging it there). I tried to have opening and closing scripts just once, but it didn't work since I added the script below. Any suggestions for what I'm doing wrong?

<script>

function moveMktoFieldsToBottom(globalOptIn) {

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

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

    try {

      elementToMove.insertBefore(".mktoButtonRow");

    } catch(err) {

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

    }

  }

}

</script>

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

<script>

MktoForms2.whenReady(function(form){

    moveMktoFieldsToBottom(['globalOptIn']);

});

</script>

Translate