How can I change this code so it works for multiple forms on one page? function…
- User
How can I change this code so it works for multiple forms on one page?
function addGclid() { var gclidParam = getParam('gclid'); var gclidFormFields = ['footer_contact','offerte_aanvragen','drawer_adviesgesprek','drawer_brochure','foobar']; // all possible gclid form field ids here var gclidRecord = null; var currGclidFormField;
var gclsrcParam = getParam('gclsrc'); var isGclsrcValid = !gclsrcParam || gclsrcParam.indexOf('aw') !== -1;
gclidFormFields.forEach(function (field) { if (document.getElementById(field)) { currGclidFormField = document.getElementById(field); } });
if (gclidParam && isGclsrcValid) { gclidRecord = getExpiryRecord(gclidParam); localStorage.setItem('gclid', JSON.stringify(gclidRecord)); }
var gclid = gclidRecord || JSON.parse(localStorage.getItem('gclid')); var isGclidValid = gclid && new Date().getTime() < gclid.expiryDate;
if (currGclidFormField && isGclidValid) { currGclidFormField.value = gclid.value; } }
window.addEventListener('load', addGclid);
- Assistant
To make the code work for multiple forms on one page, you can modify it to iterate through all the forms and their fields instead of relying on a single form field. Here's the updated code:
javascript
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.