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

141
Views
How to return innertext of sibling element

In GTM i'm trying to return the inner text of a sibling element of the clicked element.

<div class="repair-item-n ">
  <div class="repair-slide--54894d33-6c88-488f-95d7-3ec9b6a3ade4">
    <div class="restoration_wrap text-center">
      <img class="restoration-image">
    </div>
    <p class="title">Bags</p>
  </div>
</div>

For example, on click of class "restoration-image" I want to return the value "Bags".

I have multiple occurrences of this HTML on the page with varinats such as "Shoes", "Hats" etc so I want to know on click of each, which would be the respective text of the "title" class

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try this:

var images = document.querySelectorAll('.restoration-image');

images.forEach(function (image) {
    image.addEventListener('click', function (event) {
        var parent = this.parentNode;
        var nextSibling = parent.nextElementSibling;

        alert(nextSibling.innerText)
    })
}); 
about 4 years ago · Juan Pablo Isaza Report

0

This should work for GTM custom JavaScript variables.

function getImgTitle() {
  if({{event}} === 'gtm.click') {
    var image = {{Click Element}};
    var parent = image.parentNode,
        nextSibling = parent.nextElementSibling;
    return nextSibling.innerText;
  }
}
about 4 years ago · Juan Pablo Isaza Report

0

I would do this in a Custom Javascript Variable:

Click - Repair Item Title

function() {
  var clickedEl = {{Click Element}};
  if (!clickedEl) return;

  // Find the mutual parent element
  var repairItemEl = clickedEl.closest(".repair-item-n");
  if (!repairItemEl) return;

  var titleEl = repairItemEl.querySelector(".title");
  if (!titleEl) return;

  return titleEl.innerText
}}
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!