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
November 13, 2019

Works the same there, load the script at the end of the HTML.

Translate
Ashley_Bland
Level 2
November 13, 2019

Hi Sanford! 

How does your code here work if I am trying to use it on a Marketo guided landing page?

Translate
Ashley_Bland
Level 2
November 12, 2019

Thanks for the quick response! Greatly appreciated. 

Translate
SanfordWhiteman
Level 10
November 12, 2019

Doesn't matter with the above code, the entire row is reordered (including the label).

Translate
Ashley_Bland
Level 2
November 12, 2019

Hi Sanford,

How do you handle forms that have labels above fields? Just curious...Thank you!

Translate
SanfordWhiteman
Level 10
May 15, 2019

No, you cannot do this with CSS alone, because you'll break the tab order.

As noted in the comments, grab my FormsPlus::Tag and FormsPlus::Reorder libraries from MktoForms2 :: Demo :: Override Field Order 1.0.2 and you can choose any order you want.

Translate
Ellie_Cary
Level 2
May 15, 2019

Is there a way to put in custom CSS within the form theme itself to have a normal field (like opt-in) to be under the progressive profiling fields? I'm no coder mastermind and it seems like this should be something you could fix within the form itself, not on the landing page template?

Is there any sort of video example for how to implement otherwise? 

Thanks,

Ellie

Translate
June 17, 2018

That worked! Thanks Sanford. Lifesaver as always

Translate
SanfordWhiteman
Level 10
June 13, 2018

Looks like you're using the 2.0.x code actually, but you forgot to pick up the CSS from the CSS pane in CodePen.  That's essential.

Translate
June 13, 2018

Thanks Sanford.

I'm trying 1.0.x

I've tried putting the JS in the Marketo LP template or as a HTML tag in the Editor. The behavior of the form fields didn't seem to change.

go.nttict.com/testing.html

I can see from the inspector that the fields have an order assigned to them but they aren't displaying in the order specified. What am I missing?

Translate