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.
Solved! Go to Solution.
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);
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);
That works! Thank you very much.