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

219
Visualizações
Why is this eventListener firing twice upon button click?

I am very new to front end development and recently started setting EventListeners for my HTML elements with JS opposed to adding an onclick html tag as I was told onclick is bad practice (is that true?)

In the below code, the phrase that I log in the console ("Test, the input tag was clicked") always logs twice every time an input is clicked. TThe event should of course only fire once and I have no idea it prints in the console twice, what am I doing wrong?

(function () {
    window.addEventListener("load", () => {
        const documentParentElement = document.querySelectorAll(".rating-button");

        documentParentElement.forEach(element => {
            element.addEventListener("click", event => {
              const targetElement = event.target;
              console.log("Test, the input tag was clicked")
              const selectedRating = targetElement.value;
            })
        });
    });
}());
<div class="rating" data-decision-id="{{decision.id}}" data-user-id="{{decision.user_id}}">
        <input type="radio" class="rating-button" id="star-1-{{decision.id}}" name="rating" {% if decision.rating.rating == 1 %} checked {% endif %} value="1"/>
        <input type="radio" class="rating-button" id="star-2-{{decision.id}}" name="rating" {% if decision.rating.rating == 2 %} checked {% endif %} value="2"/>
        <input type="radio" class="rating-button" id="star-3-{{decision.id}}" name="rating" {% if decision.rating.rating == 3 %} checked {% endif %} value="3"/>
        <input type="radio" class="rating-button" id="star-4-{{decision.id}}" name="rating" {% if decision.rating.rating == 4 %} checked {% endif %} value="4"/>
        <input type="radio" class="rating-button" id="star-5-{{decision.id}}" name="rating" {% if decision.rating.rating == 5 %} checked {% endif %} value="5"/>     
</div>

about 4 years ago · Juan Pablo Isaza
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