Search code examples
javascriptarraysgoogle-tag-manager

Extract an Page Variant Value with Javascript


I am not a javascript expert at all and expect this to be a fairly simple task.

I am trying to grab the value from a hidden field on my page called pageVariant so I can store it as a value inside of Google Tag Manager. I just don't know how to write the javascript.

enter image description here

This is the code I have tried, but know it is not right. Any help here would be appreciated.

function() {
  var capturedText = document.querySelector("#lp-pom-form-162 > form > input[type=hidden]:nth-child(2)");
  return capturedText; }


Solution

  • First of all provide a name to your function.

    Then use the code mentioned below:

        function inputText() {
          var capturedText = document.querySelector("input[name='pageVariant']").value;
          return capturedText; 
        }