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

107
Visualizações
jQuery function is returning more events for a Modal click

I have a website where there are three different modal buttons and I wrote a variable to get the values in a data layer which is working good but when I first click on the button I get 2 events and it starts doubling up for each click afterwards.

Here is my code

function() {
  
    if ({{Click Text}} == 'STUDY DESIGN'){
  
      $(this).each(function() {
      $(this).on("click", function(){
           var imageText = $({{Click Element}}).closest('.d-md-flex').find('.image-wrapper img.image__desktop').attr("alt");
         window.dataLayer = window.dataLayer || [];
         window.dataLayer.push({
         'event': 'studyDesignInteraction',
         'action': imageText
        });
      });
  });
       return true;
     }
  }

GTM Assistant

The first click comes back with two events and it keeps doubling up after each event. How can I only apply to one click?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

$(this) in your code is window

$(this).each(function() { console.log(Object.keys(this)[0])
  $(this).on("click", function() { console.log(Object.keys(this)[0])
    var imageText = $('img').closest('.d-md-flex').find('.image-wrapper img.image__desktop').attr("alt");
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
      'event': 'studyDesignInteraction',
      'action': imageText
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="x" class="d-md-flex">
  <div class="image-wrapper">
    <img class="image__desktop" />
  </div>
</div>

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