I'm using this code to implement multiple forms on a page:
var arrayFrom = Function.prototype.call.bind(Array.prototype.slice);
var MKTOFORM_ID_ATTRNAME = "data-formId";
MktoForms2.whenRendered(function(form) {
var formEl = form.getFormElem()[0],
rando = "_" + new Date().getTime() + Math.random();
arrayFrom(formEl.querySelectorAll("label[for]")).forEach(function(labelEl) {
var forEl = formEl.querySelector('[id="' + labelEl.htmlFor + '"]'),
forElName = $(forEl).attr('name'),
labelID = "Lbl" + forElName + "_" + rando;
if (forEl) {
labelEl.htmlFor = forEl.id = forEl.id + rando;
labelEl.id = labelID;
$(forEl).attr('aria-labelledby', labelID);
}
});
});
And also this code to add a hidden field to the forms:
var arrayFrom = Function.prototype.call.bind(Array.prototype.slice);
var MKTOFORM_ID_ATTRNAME = "data-formId";
arrayFrom(config.formIds).forEach(function(formId) {
var loadForm = MktoForms2.loadForm.bind(MktoForms2,config.podId,config.munchkinId,formId),
formEls = arrayFrom(document.querySelectorAll("[" + MKTOFORM_ID_ATTRNAME + '="' + formId + '"]'));
(function loadFormCb(formEls) {
var formEl = formEls.shift();
formEl.id = "mktoForm_" + formId;
loadForm(function(form) {
form.addHiddenFields({ lastFormURL : window.location.href.replace(/https?:\/\//i, "") });
formEl.id = "";
if (formEls.length) {
loadFormCb(formEls);
}
});
})(formEls);
});
I'm trying to add recaptcha v3 to the forms now using this example: https://codepen.io/figureone/pen/JQPgPE
Can anyone help me get this code to work on multiple forms and combine it to the existing code that's being used above?
Since you're using multiple forms, you have to
It's not difficult, but tbh... if this doesn't make sense right off the bat, you should hire a developer to implement it for you. Making sure multiple form event listeners cooperate is exponentially harder than just copying-and-pasting the code for a single whenReady function.
(Also, of course, you need to do the reCAPTCHA v3 integration with Marketo Smart Campaigns, which isn't the same as the v2, or the reCAPTCHA won't be doing anything!)
Do you recommend v2 or v3 on Marketo forms?
v3 for sure.
Can we have the hidden fingerprint field already in the form, or do we need to add it using a script?
The field can already be on the form in Form Editor. The value must be set using JS, but it's fine if the input is already there.