How to get the parent name of a clicked element in GTM / Tag Manager

Simples:

Create a new Javascript Variable:

Call it whatever you like:

Here’s the javascript:

function() {
 
 var objectParentofClickedElement = $({{Click Element}}).parent();
 
 if ( objectParentofClickedElement) {
 return objectParentofClickedElement.text();
 }else 
 {return 'undefined';}
}

Preview and test the variable, for example you could click an element and see what it returns using the variables section.

Now you should be able to refer to use it in your triggers, tags and other variables.

EDIT: To get other attributes such as ID, replace .text() with something like (You’ll need jQuery in place):

.attr('id');

6 responses to “How to get the parent name of a clicked element in GTM / Tag Manager”

  1. Hi Nick, Thanks for this post. I’ve spent hours trying to get this. Can you post a vanilla js version of it for people who do not have jQuery in place. Thanks in advance!

    1. Sure thing, will do an edit! 🙂

  2. Hi there, is it possible to get the ID rather than the element text? I have tried replacing .text with .id but this doesn’t seem to work. Cheers.

    1. Try: .attr(‘id’) This is using the jQuery approach to retrieve the ID.

  3. If you are looking for a plain js solution just use: {{Click Element}}.parentNode.innerText

    1. OK Thanks! Need to check it out.

Leave a Reply

Your email address will not be published. Required fields are marked *