Form field conditional on current vlaue

Highlighted
Level 6 - Champion Alumni

Form field conditional on current vlaue

Is there a way for a form field to show up based on another field in our Marketo instance? We want to have a checkbox on our forms, but we only want it to appear if it has not already been checked. I've been searching everywhere on how to do this but can't find anything. Is there any way that I can do this? Thanks!

30 REPLIES 30
Highlighted
Level 10 - Community Moderator

Re: Form field conditional on current vlaue

Is there a way for a form field to show up based on another field in our Marketo instance? We want to have a checkbox on our forms, but we only want it to appear if it has not already been checked.

These are 2 different concepts:

  • Show/hide a field based on another field's value
  • Show/hide a field based on its own value

Which one are you actually trying to do?

Highlighted
Level 6 - Champion Alumni

Re: Form field conditional on current vlaue

Sounds like Amber Hobson is trying to only show the boolean field when the system value is NULL/False. I know you can execute this with JS, but for non-coders this is always a tough solution. And to Amber's point, OOB you cannot control visibility rules for a field based on the same field's DB values (you can for another field).

One solution that I have built/tested recently and have been meaning to share is written below.

Create a new custom field of the same type as the original field. Add "Visibility Rule" to the end of the name or something similar so its easily identifiable. Then create 2 smart campaigns to manage the visibility field when the core field changes. Like this... When core field is marked TRUE, update visibility rule field to TRUE. And the opposite. When core field is changed to FALSE, update visibility rule field to FALSE. Once this is done, you'll need to batch update the visibility rule field to match the core field for our existing database. Now on the form, add both fields and make the visibility rule field hidden (allow pre-fill). Then add a form visibility rule to the core field to only show the core field when the visibility field is FALSE.

This makes it so the core boolean field is only shown on the form when the visibility field is FALSE. And thus, allows you to show/hide the field based on the system value of the same field.

Caveats:

- Based on how fast your smart campaigns are running, the field may not remove from the form immediately.

- Field will still show if cookies are cleared or user visits your site on a new machine.

- Requires you to duplicate a field which isn't ideal and can be confusing if not given an intuitive name.

I know i'll catch some flak from the people who are JS proficient (cough Sanford Whiteman​) but for me, this solution was easy to implement and required no JS.

Let me know what you think of this solution!

Highlighted
Level 10 - Community Moderator

Re: Form field conditional on current vlaue

Then create 2 smart campaigns to manage the visibility field when the core field changes. Like this... When core field is marked TRUE, update visibility rule field to TRUE. And the opposite. When core field is changed to FALSE, update visibility rule field to FALSE. Once this is done, you'll need to batch update the visibility rule field to match the core field for our existing database.

If anything, this is the part that is easier to manage on the client via JavaScript.

I understand the appeal of (and fully support) using the built-in VR logic as opposed to reproducing all that logic in code, if you're not a coder.

But the above is a ton of work to just get a hidden field to have the same value as the visible field on the form.  You can do this in one line of JavaScript, and no need for a Smart Campaign:

form.setValues({ hiddenFieldForVisibilityRule : form.getValues().visibleCheckboxField });

Highlighted

Re: Form field conditional on current vlaue

The advantages of doing in a JS line of code are:

  • That you are 100% sure that the values remain in sync in Marketo, whatever delay happens on your smart campaigns.
  • In the activity log, the hidden field will be part of the "filled out form" activity, with timestamp and IP address, which might be very useful in case of audit (remember the GDPR need for an audit trail).

We usually use that same technique to set the opt-out field based on the opt-in field.

-Greg

Level 4

Re: Form field conditional on current vlaue

Sanford Whiteman​ I'm trying to achieve the following with my Marketo forms:

  • Show/hide a field based on its own value

You say the following JS would make this work:

form.setValues({ hiddenFieldForVisibilityRule : form.getValues().visibleCheckboxField });

As I'm a non-coder, could you explain how and where to add this line of code? I assume in the "edit custom CSS" part of the form?

And how should I adjust the line of code so that it refers to field X and Y and not to field Z?

Thanks a lot for your help! Would be great if we could achieve this without a cumbersome smart campaign.

- Tom

Highlighted
Level 10 - Community Moderator

Re: Form field conditional on current vlaue

As I'm a non-coder, could you explain how and where to add this line of code? I assume in the "edit custom CSS" part of the form?

Nope, it's JavaScript, so you should put it in a separate <script> block.  You can put the <script> before the closing </body> tag on a Marketo LP, or right after the embed code on a non-Marketo LP.

<script>

MktoForms2.whenReady(function(form){

  form.setValues({

    hiddenFieldForVisibilityRule : form.getValues().visibleCheckboxField

  });

})

</script>

And how should I adjust the line of code so that it refers to field X and Y and not to field Z?

Replace "hiddenFieldForVisibilityRule" and "visibleCheckboxField" with the real field names on your form.

You can use your browser's F12 Inspector to get the field names.

Highlighted
Level 4

Re: Form field conditional on current vlaue

Hi Sanford,

Many thanks for helping out. I'm gonna add the JS this week, and test it out. Hope it works!

Best,

Tom

Highlighted
Level 4

Re: Form field conditional on current vlaue

Hi Sanford,

Thank you for sharing such a great solution. I understand the logic and the js code which will capture the value from the "visibleCheckboxField" combined with 2 SCs. However, for our case, we will show the "hiddenFieldForVisibilityRule" -- "Opt-in" field, when leads only select any EU countries. So "Country" will be our  "visibleCheckboxField". In this case, I can't create the smart campaign to capture "T/F" value which will alter the value of "Opt-in" field. The goal here is we would like to not show "opt-in" field on the form, when the lead already opted in. And I can't do  "visibleCheckboxField" and "hiddenFieldForVisibilityRule" both are the same "opt-in" field. Do you have any suggestion for our case? I was trying Dan's solution, haven't got succeed yet. 

Best,

Becky

Highlighted
Level 1

Re: Form field conditional on current vlaue

Hi Becky - Were you ever able to find a resolution to your issue? We have the exact same problem where we're using visibility rules on our consent field such that the consent field only appears if the user identifies they are from the EU. I also tried the solutions noted here with the custom javascript, and granted I'm not the most technical person, haven't had luck.

Thanks,
Laura