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

153
Visualizações
My js file keeps on repeating my the previous list item. for example if i add a text on my form, the previoius list item repeat itself with new one
let leadTracker = [];
const inputText = document.getElementById("input-text");
const inputButton = document.getElementById("input-button");
const Listing = document.getElementById("listings");

inputButton.addEventListener("click", function () {
  leadTracker.push(inputText.value);
  clicker();
});

function clicker() {
  for (let i = 0; i < leadTracker.length; i++) {
    Listing.innerText += leadTracker[i];
    console.log(leadTracker);
  }
}

I added bro to the array and then back but bro is repeated. This is exactly the same code I read online but it is still not working fine for me.

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

0

You have to ways to do this : first you have to not use for loop in the function

function clicker() {
    Listing.innerText += leadTracker[leadTracker.length-1];
    console.log(leadTracker);
  
}

or you can remove all innerText from the element then re-write it again :

function clicker() {
  Listing.innerText = "";
  for (let i = 0; i < leadTracker.length; i++) {
    Listing.innerText += leadTracker[i];
    console.log(leadTracker);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Each time you click the button you take the content of the text field and add it to the leadTracker array. So the array grows with each click, longer and longer. So far so good.

But in the the clicker function, which also runs on every click, you take the entire content of the array and append it to Listing. They array is not cleared in between clicks, so old items in it will be printed again.

You can either skip using an array at all:

inputButton.addEventListener("click", function () {
  Listing.innerText += inputText.value;
});

or replace the inner text of Listing instead of appending it, as suggested in other answers.

about 4 years ago · Juan Pablo Isaza Relatório

0

Each time you call clicker it's going to add the whole array to Listing, including stuff that's already there.

Did you mean to clear out Listing at the beginning of clicker, before the for loop?

Listing.innerText = ""

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