0

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?

Rob E.
  • 21
  • 3
  • I wouldn't recommend using `document.write();` at all. If you want to test/debug then use `console.log();` and open the browser console. Maybe this post will help you understand http://stackoverflow.com/questions/20273805/document-write-removes-other-html Also `getElementsByName()` returns a collection of all elements in the document with the specified name - NodeList object. – NewToJS Jan 26 '17 at 12:49
  • 1
    `document.write()` isn't what you think it is. – emerson.marini Jan 26 '17 at 12:51
  • I put the document.write() just to see if the parmeter get extracted successfully – Rob E. Jan 26 '17 at 12:59

2 Answers2

0

try following

document.getElementsByName("utm_source")[0].value = source;
document.getElementsByName("utm_medium")[0].value = medium;
document.getElementsByName("utm_campaign")[0].value = campaign;

document.getElementsByName() returns a node list object hence you have to give it a treatment like array. Reference

K D
  • 5,889
  • 1
  • 23
  • 35
  • 1
    Going to explain the changes you have made? Some people like to understand why the current attempt isn't functioning and why `[0]` is allowing this to function. – NewToJS Jan 26 '17 at 12:52
  • tried this but it does not work. anything else that might be wrong? – Rob E. Jan 26 '17 at 13:02
  • make sure your script is at the bottom of page just before

    tag. try to alert(document.getElementsByName("utm_source").length)

    – K D Jan 26 '17 at 13:04
  • did you got any hint? – K D Jan 26 '17 at 14:32
0

ok, now it works.

I used the code

document.getElementsByName("utm_source")[0].value = source;
document.getElementsByName("utm_medium")[0].value = medium;
document.getElementsByName("utm_campaign")[0].value = campaign; 

and moved that code from the header to right before the closing .

now the values of the utm parameters show up

Rob E.
  • 21
  • 3