I'm trying to set the value of a hidden field in my form through a URL parameter.
Here is the form:
<form accept-charset="UTF-8" action="/thanks" class="infusion-form" method="POST">
<input name="inf_field_LeadSourceId" type="hidden" value="null" />
<input class="infusion-field-input" id="inf_field_FirstName" name="inf_field_FirstName" placeholder="First Name *" type="text" required/>
<input class="infusion-field-input" id="inf_field_Email" name="inf_field_Email" placeholder="Email *" type="text" required/>
<div class="infusion-submit">
<button class="infusion-recaptcha" type="submit">Submit</button>
</div>
</form>
I need to set the value of this field, specifically:
<input name="inf_field_LeadSourceId" type="hidden" value="null" />
with a url parameter.
Ideally, I would like it to be something like this:
https://website.com/page?leadsource=123
So it would set that field to have the value of "123"
I tried doing this using the javascript code below, but no luck :(
<script>
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? 120 : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var LeadId = getParameterByName("leadsource");
jQuery(".infusion-form input[name='inf_field_LeadSourceId']").val(LeadId);
</script>
Any advice on how I can tweak my javascript to prefill my form with the URL parameter?