SOLVED

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

Go to solution
Highlighted
Anonymous
Not applicable

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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Level 10 - Community Moderator

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

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

View solution in original post

2 REPLIES 2
Highlighted
Level 10 - Community Moderator

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

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

View solution in original post

Highlighted
Anonymous
Not applicable

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

That works! Thank you very much.