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

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

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 Relatório

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 Relatório

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