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

121
Visualizações
event listener's event is being used more than once

I have this weird bug that whenever I want to add an item/card to the list and I cycle between clicking the text input and then clicking outside of it, the list won't add only one item but the number of times I pressed in and out.

enter image description here

const main = document.querySelector('main')

main.addEventListener('click', (e) => {

  // Name a group

  if (e.target.id === 'groupNameInput') {
    console.log(e.target)
    let groupNameInput = e.target
    groupNameInput.addEventListener('change', () => {
      let groupName = groupNameInput.parentElement
      console.log(groupNameInput.value)
      groupNameInput.style.display = 'none'
      groupName.innerText = `${groupNameInput.value}`
      groupName.parentElement.lastElementChild.classList.remove('notAssigned')
      groupName.parentElement.lastElementChild.style.display = 'inline'
    })
  } else if (e.target.classList == 'addCardInput' && e.target.classList != 'notAssigned') {

    let addCardInput = e.target
    let taskUl = addCardInput.parentElement.querySelector('.taskUl')
    addCardInput.addEventListener("keydown", function(event) {
      if (event.key == 'Enter') {
        console.log("I'm in!")
        let newLi = document.createElement('li')
        newLi.innerHTML = `${event.target.value}`
        taskUl.appendChild(newLi)
      }
    })
  }
})
<main>
  <section class="groupSection">
    <span class="groupName">
                        <input type="text" autocomplete="false" id="groupNameInput" placeholder="name">
                    </span>
    <ul class="taskUl">
    </ul>
    <input type="text" class="addCardInput" placeholder="Add a card...">
  </section>
  <section class="addItemsSection">
    <button id="addItemsBtn">
                        <img src="./srcs/add_Icon.png" alt="" id="addItemsImage">
                    </button>
  </section>
</main>

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