How to use variables in the object of setValues() method? | Community
Skip to main content
February 8, 2018
Solved

How to use variables in the object of setValues() method?

  • February 8, 2018
  • 1 reply
  • 3132 views

I have several selectmenues whose values will be changed by a jQuery function. Since Marketo form does not respond to that kind of DOM method. I have to write an additional line of code by using Marketo's Forms API setValues() method in that jQuery function to let Marketo form know what happened.

Normally, for a single selectmenu, the code would look like:

MktoForms2.getForm(1499).setValues({ Country: 'United States' });

But there are other selectmenues, not just Country. So I would like to make the object name and value as variables:

MktoForms2.getForm(1499).setValues({ this.name: this.value });

this in the above code refers to <select> element which is being changed. But the above code is yielding an error (Uncaught SyntaxError: Unexpected token .)

Any advice would be much appreciated. Thanks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

So I would like to make the object name and value as variables:

MktoForms2.getForm(1499).setValues({ this.name: this.value });

JavaScript never works this way.  It has nothing to with the Marketo JS API in particular.

Dot-property names are (if you want anything close to real-world browser compatibility) constants.

If you want dynamic property names, use bracket notation.

var mktoFields = {};

mktoFields[this.name] = this.value;

MktoForms2.getForm(1499).setValues(mktoFields);

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
February 8, 2018

So I would like to make the object name and value as variables:

MktoForms2.getForm(1499).setValues({ this.name: this.value });

JavaScript never works this way.  It has nothing to with the Marketo JS API in particular.

Dot-property names are (if you want anything close to real-world browser compatibility) constants.

If you want dynamic property names, use bracket notation.

var mktoFields = {};

mktoFields[this.name] = this.value;

MktoForms2.getForm(1499).setValues(mktoFields);

February 8, 2018

That works! Thank you very much.