Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda