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

149
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
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