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 this code work for multiple forms on one page, you'll need to modify the function to iterate through all forms on the page and check if the current form's ID is in the
gclidFormFieldsarray. Here's an updated version of the 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.