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

80
Views
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 answers
Answer question

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 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!