I am trying to set the value of an input field to the value of a utm_parameter from the url
I have this code in the head of the page to extract the values of the parameters and set the value of the fields
<!-- Get UTM Parameters Code -->
<script>
// Parse the URL
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Give the URL parameters variable names
var source = getParameterByName('utm_source');
var medium = getParameterByName('utm_medium');
var campaign = getParameterByName('utm_campaign');
// Put the variable names into the hidden fields in the form.
document.getElementsByName("utm_source").value = source;
document.getElementsByName("utm_medium").value = medium;
document.getElementsByName("utm_campaign").value = campaign;
</script>
<!-- End Get UTM Parameters Code -->
this is the code that is in the body of the page.
<form>
<input type="text" name="utm_source" id="utm_source" value="">
<input type="text" name="utm_medium" id="utm_medium" value="">
<input type="text" name="utm_campaign" id="utm_campaign" value="">
</form>
The parameters get extracted sucessfully but they are not set as the value of the input fields.
Is there something wrong with the code?