0

I've trying to customise a script that normally triggers a GTM tag to populate a form's hidden field with a variable (session_url_campaign).

Here is the script:

<script>
(function () {
var value = “{{session_url_campaign}}” 
var selector = "input[name='859253_16342pi_859253_16342']" 
var field = document.querySelector(selector)
if(field){ field.value = value||"(none)"; }
})();
</script>

I'd like to use this tag to populate all my forms, but unformatunately, my CRM - Pardot - uses form specific names when a same field is use across different form. The name of my hidden field in the above script is 859253_16342pi_859253_16342, but it could be 859253_16342pi_859253_16343 in another form.

Is there a way to use a "OR" logic for my name?

After some research I've found some mention of ||. Would solving my problem as easy as adding between the different values, as below?

<script>
(function () {
var value = “{{session_url_campaign}}” 
var selector = "input[name='859253_16342pi_859253_16342||859253_16342pi_859253_16343']" 
var field = document.querySelector(selector)
if(field){ field.value = value||"(none)"; }
})();
</script>
6
  • 1
    so, you want OR is CSS selector - "input[name='first'],input[name='second']" Commented Jan 26 at 4:04
  • 1
    or "input:is([name='first'],[name='second'])" is slightly better Commented Jan 26 at 4:11
  • The example field names you have given appear to differ only in the very last digit - does this perhaps mean, these fields could all be identified by a common, but unique (to this type of field that you need to target) prefix? If so, you could do prefix matching with the attribute selector, and would not have to list all the different field names explicitly. (Or maybe a combination of common prefix and [type=hidden])
    – CBroe
    Commented Jan 26 at 9:28
  • Don't use "smart quotes" in code. JavaScript only understands straight quotes.
    – Barmar
    Commented Jan 26 at 18:10
  • @CBroe Thanks for your answers. The second value I used as an example is just a dummy value. In reality, I think that all fields use a shared prefix (the first 3 groups of characters are always the same for all forms generated ini my account), so the combination of a prefix and [type=hidden] will be worth investigating. Any chance you could tell me what would define this combination? Thanks again. Commented Jan 27 at 0:02

0