Advertisement
NonplayerCharacter

JS | Add listener to every member of array/nodelist

Mar 8th, 2022 (edited)
1,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var allAs = document.querySelectorAll('a');
  2. allAs.forEach(
  3.     function(el){
  4.         el.addEventListener(
  5.             "mousedown",
  6.             function(){
  7.                 dataLayer.push({
  8.                     'event' : 'gtm_link_click',
  9.                     'eventAction' : this.innerText.toLowerCase(),
  10.                     'eventLabel' : this.getAttribute('href').toLowerCase()
  11.                 });
  12.                 this.removeAttribute('href'); // for testing only so we don't follow links
  13.             });
  14. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement