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

145
Vistas
To do list clear button

im creating to-do list without any tutorial and i stucked when i tried to code clear button. I have a problem because this code removing only half of 'li' items in my list. I checked length of document.querySelectorAll('li) and it return correct value of list length , and i think in each loop execution i delete first element because document.querySelector('li) return only first element. Could you help me? And another question : Is somewhere in web program that can i see step by step exection of my code with DOM? I found few sites but there i can only debug code without html and css.

There is my code:

let clear = document.querySelector('.clear');
let input = document.querySelector('.input');
let submit = document.querySelector('.submit');
let list = document.querySelector('.list');

submit.addEventListener('click', function() {

  const el = document.createElement('li');

  list.appendChild(el);

  let items = document.querySelectorAll('li');

  for (i = 0; i < items.length; i++) {
    items[items.length - 1].textContent = input.value;

  }
});

clear.addEventListener('click', function() {
  console.log(document.querySelectorAll('li').length);

  for (i = 0; i < document.querySelectorAll('li').length; i++)
    list.removeChild(document.querySelector('li'));
  console.log(list);
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-direction: column;
  max-height: 100vh;
  max-width: 90vw;
}

h1 {
  text-align: center;
}

.wrap {
  padding: 50px;
  border: 2px solid black;
  border-radius: 20%;
  background-color: red;
}

.list {
  width: 100%;
  margin-left: 100px;
  font-size: 2rem;
  font-weight: bold;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="style.css" />
</head>

<body>
  <div class="wrap">
    <h1>To do list</h1>
    <input type="text" placeholder="Add an item!" class="input" />
    <button class="submit">Submit</button>
    <button class="clear">Clear List</button>
  </div>
  <ol class="list"></ol>
  <script src="script.js"></script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In your case, your loop was calculating its length each time of the loop while i was still incrementing

I you had two elements, it would have deleted the first item, then at the second loop, the list would have a length of 1 and i would be equals to 1 so the loop would break

You can use a for of loop instead of a for i loop and remove the child by reference

clear.addEventListener('click', function() {
  const childs = document.querySelectorAll('li')
  for (const child of childs){
    list.removeChild(child)
  }
});

Note : this can also be done using a for i loop if you instantiate the list

clear.addEventListener('click', function() {
  const childs = document.querySelectorAll('li')
  for (let i = 0; i < childs.length; i++){
    list.removeChild(childs[i])
  }
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

I invent a new way to solve it

clear.addEventListener('click', function () {
  for (i = 0; i < document.querySelectorAll('li').length + i; i++)
    list.removeChild(document.querySelector('li'));
  console.log(list);
});

In each iteration I add 'i' . So i update length

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