vendredi 24 juin 2016

GTM/Javascript - Search up DOM to find Parent h2

What i'm trying to accomplish is to find if the click element has a h2 tag, if not, then move to the parent and perform the same check until a value is found and return the value. When the href link is clicked, I need Google Tag Manager to set the parent h2 text as the variable so it can be used in Event Tracking. Here's an extract of the HTML code; <div id="contentRight" class="right"> <div class="widget blue"> <h2>Link Title</h2> <div style="clear:both;"> <a href="https://www.websitelink.com" class="buttonOrange" target="_self" >Find out more <i class="fa fa-chevron-right" aria-hidden="true"></i> </a> </div> </div> </div> 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; function(){ var element = {{Click Element}}; while(element.querySelector('h2').innerText === undefined); { element = element.parentNode; } return element.querySelector('h2').innerText;} Can anyone shed some light on what i'm doing wrong, or provide some suggestion how this may be done?

Aucun commentaire:

Enregistrer un commentaire