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();
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
to select this element use script like below
Replace first_name with your field's API name