I am trying to pull the Alt attribute from the HTML image element and there are 10 such parent DIV element having img element as child. The problem with current setup is in GTM iam not able to get the Name of the Expert when someone clicks on the Image element.
to overcome this i am using the below java-script as a custom javascript variable in GTM to capture the Alt attribute but its coming as undefined.
basically iam attaching a click event to all the DIV elements so when a user clicks on any of the DIVs i use the currenttarget and access the child element to get the ATT. i had tested the same in the console and it worked but as a Custom Javascript variable iam getting undefined.
Can you please help.
HTML code
<div class="card__media-overlapping__media">
<a href="https://capgemini.aws.hmn.md/experts/testing/wouter-koppen/">
<img src="https://placehold.it/617x347" alt="Wouter Koppen">
</a></div>
Custom Javascript variable in GTM
function(){
try{
var divHead= document.getElementsByClassName('card__media-overlapping__media');
var eventhandlerdoc = function(event){
var imgALT=event.currentTarget.firstElementChild.firstElementChild.getAttribute('alt');
return imgALT;
}
for(var index=0; index < divHead.length; index++){
divHead[index].addEventListener('click',eventhandlerdoc,true);}
}
catch(e){
return "n/a"
}
}

