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();