Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda