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

112
Visualizações
Add event listener(mousedown, mousemove, mouseup )to a dynamic div with plain vanilla javaScript only

I am developing a card game to play in the browser. This is a project for my javascript module so I am learning my way out of it. I need to move a card from place to place, this is not difficult if the targeted div exists in index.html but I need to target the children and they are all dynamically created. when I change the target to the children with a new class the event listener broke. I need help to successfully target these dynamic children.

const card = getDOMElement(FLIPPED_CARDS_ID);
card.addEventListener("mousedown", mouseDown);

  const mouseDown = (e) => {
  let prevX = e.clientX;
  let prevY = e.clientY;

  const mouseMove = (e) => {
    let newX = prevX - e.clientX;
    let newY = prevY - e.clientY;
    const rect = card.getBoundingClientRect();
    card.style.left = rect.left - newX + "px";
    card.style.top = rect.top - newY + "px";
    prevX = e.clientX;
    prevY = e.clientY;
  };
  window.addEventListener("mousemove", mouseMove);
  const mouseUp = () => {
    window.removeEventListener("mousemove", mouseMove);
    window.removeEventListener("mouseup", mouseUp);
  };
  window.addEventListener("mouseup", mouseUp);
};

the div affected is the following

  const flippedCardsDiv = getDOMElement(FLIPPED_CARDS_ID);
  const newFlippedCard = document.createElement("div");
  newFlippedCard.classList.add("new-flipped-card");
  const flippedCards = deck.dealOneCard();
  newFlippedCard.innerHTML = getNewCardHtml(flippedCards[0]);
  flippedCardsDiv.appendChild(newFlippedCard);

I hope I was clear enough explaning my problem . Thanks in advance.

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