Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

82
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda