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

197
Visualizações
When I append an element it does it twice rather than once

I am trying to make multiple comment boxes for a website. The first click to create the text area and submit button works fine. However when I click another it appends the text area and submit button twice - creating two text areas and submit buttons and then three and so on..

If you go to https://alexpd93.github.io/FAC-Website/ and click on various comment buttons it should make more sense.

I would like it so that each time I click on the comment button, it only creates one text area for each section.

How can I fix this?

function addComment(element) {
  const boxContainer = element.parentNode.parentNode.parentNode;
  const commentContainer = element.parentNode;
  commentContainer.classList.add("comment-container-after-click");

  const commentBox = document.createElement("textarea");
  commentBox.classList.add("comment-box-after-click");
  commentBox.placeholder = "What are your thoughts?";
  commentBox.innerHTML = "";

  const submitComment = document.createElement("button");
  submitComment.classList.add("submit-comment-after-click");
  submitComment.innerHTML = "Comment";

  commentContainer.append(commentBox, submitComment);

  submitComment.onclick = function submitComment() {
    let comment = commentBox.value;
    const newComments = document.createElement("p");
    boxContainer.appendChild(newComments);
    newComments.innerHTML = `${comment}`;
    commentBox.value = "";
  };
}

function comment(event) {
  const commentButton = event.target;
  commentButton.style.display = "none";
  const commentIcon = document.getElementsByClassName("comment-icon");

  const iconArray = Array.from(commentIcon);
  iconArray.forEach((icon) => {
    if (icon.nextElementSibling.style.display === "none") {
      icon.style.display = "none";
      addComment(commentButton);
    }
  });
}
<div class="comments-container" id="comments">
    <img id="commentIcon" class="comment-icon" src="Images/Comment.png" alt="comment icon">
<button class="comment-button" id="commentButton" onclick="comment(event)" > Comment </button>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

After playing around with the website I believe I understood the problem. You are calling addComment() for all elements with class comment-icon. And due to your (tbh) little bit weird handling of addComment() this adds a comment to every single one of this elements.

I've corrected the error below and marked my changes with comments.

function addComment(element) {
  const boxContainer = element.parentNode.parentNode.parentNode;
  const commentContainer = element.parentNode;
  commentContainer.classList.add("comment-container-after-click");

  const commentBox = document.createElement("textarea");
  commentBox.classList.add("comment-box-after-click");
  commentBox.placeholder = "What are your thoughts?";
  commentBox.innerHTML = "";

  const submitComment = document.createElement("button");
  submitComment.classList.add("submit-comment-after-click");
  submitComment.innerHTML = "Comment";

  commentContainer.append(commentBox, submitComment);

  submitComment.onclick = function submitComment() {
    let comment = commentBox.value;
    const newComments = document.createElement("p");
    // why append to the box container? Then all comments from different textareas will be merged
    // boxContainer.appendChild(newComments);
    commentContainer.appendChild(newComments);
    newComments.innerHTML = `${comment}`;
    commentBox.value = "";
  };
}

function comment(event) {
  const commentButton = event.target;
  commentButton.style.display = "none";
  const commentIcon = document.getElementsByClassName("comment-icon");

  // add the comment once not to every comment box available
  addComment(commentButton);

  const iconArray = Array.from(commentIcon);
  iconArray.forEach((icon) => {
    if (icon.nextElementSibling.style.display === "none") {
      icon.style.display = "none";
      // this causes that the comment is added multiple times
      // addComment(commentButton);
    }
  });
}
<div div="all-comments">
  <div class="comments-container" id="comments">
    <img id="commentIcon" class="comment-icon" src="https://dummyimage.com/100x100/000/fff" alt="comment icon">
    <button class="comment-button" id="commentButton" onclick="comment(event)"> Comment </button>
  </div>
  <div class="comments-container" id="comments">
    <img id="commentIcon" class="comment-icon" src="https://dummyimage.com/100x100/000/fff" alt="comment icon">
    <button class="comment-button" id="commentButton" onclick="comment(event)"> Comment </button>
  </div>
  <div class="comments-container" id="comments">
    <img id="commentIcon" class="comment-icon" src="https://dummyimage.com/100x100/000/fff" alt="comment icon">
    <button class="comment-button" id="commentButton" onclick="comment(event)"> Comment </button>
  </div>
</div>

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