Hey Guarav (and all),
Good news -- I think I’ve finally figured out how to make this work for me. Don’t ask me why this works, and the other code doesn’t
I just know that it is working. Some things I figured out…
* The code seems to be more cooperative when I specify the field in question with its ID, rather than its name.
* During the troubleshooting stage I switched the field from hidden to visible, just so I could see what was happening. Once I got it working and then switched it back to a hidden field, it stopped working. I made it “hidden” again by leaving it visible with HubSpot’s settings, but applying some CSS on the front end to make the field and its constituent parts (label, field, and all) hidden from view.
Here’s what I got to work:
<script charset=“utf-8” type=“text/javascript” src=“//js.hsforms.net/forms/embed/v2.js”></script>
<script>
window.hsFormsOnReady = window.hsFormsOnReady || [];
window.hsFormsOnReady.push(()=>{
hbspt.forms.create({
region: “na1”,
portalId: “xxxx”,
formId: “xxxx”,
onFormReady: function(form) {
setTimeout(function() {
// Select the “asset_requested” input field by its ID
var assetField = document.getElementById(‘asset_requested-626f9679-5acb-457a-a47d-d6941897d1e0’);
if (assetField) {
// Set the value
assetField.value = “SCE-VAA-EBK”;
}
}, 1000); // Adjust timeout as needed for your form loading speed
}
})});
</script>
And here’s the front-end css code:
.hs_asset_requested {
display:none;
}
I hope this helps someone else! Cheers ![]()