title: GTM/Javascript - Search up DOM to find Parent h2
body: <p>What i'm trying to accomplish is to find if the click element has a <code>h2</code> tag, if not, then move to the parent and perform the same check until a value is found and return the value.</p>
  
  <p>When the href link is clicked, I need Google Tag Manager to set the parent <code>h2</code> text as the variable so it can be used in Event Tracking.</p>
  
  <p><em>Edit: I cannot use jQuery code for this sorry.</em></p>
  
  <p>Here's an extract of the HTML code;</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="contentRight" class="right"&gt;
    &lt;div class="widget blue"&gt;
        &lt;h2&gt;Link Title&lt;/h2&gt;
      &lt;div style="clear:both;"&gt;
        &lt;a href="https://www.websitelink.com" class="buttonOrange" target="_self" &gt;Find out more 
          &lt;i class="fa   fa-chevron-right" aria-hidden="true"&gt;&lt;/i&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
    </code></pre>
  </div>
  </div>
  </p>
  
  <p>After spending a few hours looking at various post all over the internet, I currently have a mash up of code that isn't working;</p>
  
  <pre><code>function(){    
    var element = {{Click Element}};
      while(element.querySelector('h2').innerText === undefined);
   {
       element = element.parentNode;
   }
   return element.querySelector('h2').innerText;}
  </code></pre>
  
  <p>Can anyone shed some light on what i'm doing wrong, or provide some suggestion how this may be done?</p>