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

81
Visualizações
return innerhtml of a p class when a button class is clicked custom javascript google tag manager

I am trying to create a custom javascript in Google Tag Manager to return the p class text element whenever a specific button is clicked within the div. But my script keeps returning the same initial value.

In GTM, I set the trigger element click based on class "myButton". and for the variable, I have this custom javascript code.

function ()

  {
   var x = document.getElementsByClassName("test-here")[0].innerHTML;
 
    return x;
  }

HTML:

<div id = "mydivd1" class = "test">
<p class = "test-here">   I want to return this text 1</p>
<button class = "mybutton"></button>
</div>


<div id = "mydiv2" class = "test">
<p class = "test-here">   I want to return this text 2</p>
<button class = "mybutton"></button>
</div>

How do I return the correct p text element based on the current button class clicked?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Rather than using document in the function, use the relative context of the button.

function (e)

  {
   var x = e.target.parentNode.getElementsByClassName("test-here")[0].innerHTML;
 
    return x;
  }
about 4 years ago · Santiago Gelvez 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