Recently, I've been working on setting a hidden Pardot field within an iframe.
Oddly enough, when I manually input the query selector in my Chrome console, I successfully locate the element. However, when running this code snippet (embedded in the <head>
), it returns null.
I came across information suggesting that using numbers at the start of an ID for an element can lead to issues, but unfortunately, the ID is automatically generated and cannot be altered programmatically.
Any thoughts on where I might be going 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();