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

127
Views
function to return innerhtml of clicked class in google tag manager

I need a variable in Google Tagmanager which returns the innerhtml of a clicked class. Since there are more elements with this class I have to add an event listener to the classes and return the innerhtml of the clicked element.

Unfortunately my code keeps returning 'undefined'.

document.querySelectorAll(".myclass").foreach(function (item) {
  item.addEventListener("click", function(event) {
    return event.innerHTML;
  })
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Okay, so a variable that returns inner html of a clicked element (you don't click classes, you click elements).

  1. Make a click trigger where you would use whatever CSS selector you need it to work for.

  2. Make a Custom JS variable like so:

    return {{Clicked Element}}.innerHTML;

  3. Now you can make a tag where you would use your trigger and your CJS variable.

No need to add your own event listeners when GTM is perfectly capable of doing it for you. While you certainly still can add your event listeners and pass the data from them to GTM through the dataLayer, you're increasing the complexity needlessly.

about 4 years ago · Juan Pablo Isaza 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!