Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
Select Every One not just first

I'm working on a custom java script variable for tag manager to track the navigation of my page. The script is only working for the very first element of the kind because of document.queryselect. document.queryselectAll is naturally not solving the problem. It would be great if you could help me:

function(){
   var elem = {{Click Element}};
   var menu = document.querySelector("ul.nav.meta-nav.justify-content-end");
   if(menu.contains(elem)){
     return "nav_header";
   };
var menu = document.querySelector("div.product-item");
   if(menu.contains(elem)){
     return "itemlist";
   };
var menu = document.querySelector("a.btn");
   if(menu.contains(elem)){
     return "cta_button";
   };
var menu = document.querySelector("footer.footer*");
   if(menu.contains(elem)){
     return "footer";
    }
   return "other";
}
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Element.querySelectorAll() returns a NodeList. It's like an array. So a typical usage would be:

var elem = {{Click Element}};
document.querySelectorAll(selector).forEach(function(el) {
  if (el.contains(elem)) {
    return el.getAttribute("data-something");
  }
})
about 4 years ago · Santiago Trujillo Report

0

function(){
  var elem = {{Click Element}};
  var type = "other"

  var selectors = {
    "ul.nav.meta-nav.justify-content-end" : "nav_header",
    "div.product-item" : "itemlist",
    "a.btn" : "cta_button",
    "footer.footer *"   : "footer"
  }

  for (const [key, value] of Object.entries(selectors)) {
    var domMenu = document.querySelector(key)
    if(domMenu && domMenu.contains(elem)){
       type = value
     };
  }

  return type;
}

Can not test from my end but it might work.

Just make sure the selectors is good and the click element should not fall into more than one selectors.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!