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

151
Visualizações
How to merge two addEventListener to single one click?

todo list app preview

I have two addEventListener, the one is to change the text color and the other one to change the circle color.

Condition 1: When I clicked the circle, both of text and circle color was changed.

Condition 2: The problem was when I clicked the text, its color was changed, but the circle didn't change.

So can I merge it into just only one addEventListener at the circle? and disable click for the text? Or can I make the text work like I clicked the circle?
Github Link
Preview

let lists = document.querySelectorAll(".list-item");
let circles = document.querySelectorAll(".fa-circle");

// Click the text
Array.from(lists).forEach((list) => {
  list.addEventListener("click", () => {
    
    list.classList.add("done");
  });
});

// Click the circle
Array.from(circles).forEach((circle) => {
  circle.addEventListener("click", () => {
    
    circle.classList.remove("fa-regular");
    circle.classList.add("fa-solid", "fa-circle-check");
  });
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Depends on what you want. By the way, what does your DOM tree look like? is .list-item a parent of/ or a sibling to '.fa-circle'?. If you want to dispatch the event once the user clicks anywhere on a list item. Then you can use the parent, like this:

const listParents = document.querySelectorAll('.list-parent')

function handleItemClick(event) {
  const listParent = event.target
  
  const listText = listParent.querySelector('.list-item')
  const listCircle = listParent.querySelector('.list-circle')
  
  listText.classList.toggle('done')

  listCircle.classList.toggle('fa-circle')
  listCircle.classList.toggle('fa-regular')
  
  listCircle.classList.toggle('fa-circle-check')
  listCircle.classList.toggle('fa-solid')


  l 
}

Array.from(listParents).forEach(listParent => {
  listParent.addEventListener('click', handleItemClick)
})
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