0

I'm trying to set a hidden Pardot field (inside an iframe).

When I manually enter the query selector in my chrome console I'm able to get the element, but when I run this code snippet (included in <head>) I get null.

I read that starting with numbers (only option available) as an ID for an element can cause problems, but I'm unable to set the ID as it is programmatically generated.

Any idea what I'm doing wrong?


  function setGclid() {
    var id = "971073_73591pi_971073_73591"

    console.log(document.getElementById(id)) //returns null

    //also tried this: console.log(document.querySelector("#\\39 71073_73591pi_971073_73591")) //returned null 


    if (!document.getElementById(id)) {
      console.log('checking')
      window.setTimeout(setGclid, 100); /* this checks the flag every 100 milliseconds*/
    } else {
      var gclid = getCookie('gclid');
      document.getElementById(id).value = gclid

      console.log('complete')
    }
  }
  setGclid();
Justin Young
  • 2,393
  • 3
  • 36
  • 62

1 Answers1

0

Id is dynamic (will keep on changing) for each pardot form fields.

Use classname instead, by default the input tag will come under p tag with that field's api name as it's class name, like below

<p class="form-field  first_name pd-text required required-custom    ">
    <label class="field-label" for="494811_96722pi_494811_96722">First Name *</label>
    <input type="text" name="494811_96722pi_494811_96722" id="494811_96722pi_494811_96722" value="" class="text" size="30" maxlength="40" onchange="" onfocus="">
</p>

to select this element use script like below

document.querySelector("p.first_name input")

Replace first_name with your field's API name

Mohan
  • 11
  • 3