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

120
Vistas
To Do list with check button

I want to make my own To Do list using JavaScript and localStorage. After writing the input and pressing the send button, the item will be added and display on the screen. A check button will appear next to the item. After pressing the check button, I want the class to be added. I want add css (opacity, line...). But when I press a check button, the console shows this error: Uncaught TypeError: Cannot read properties of undefined (reading 'add') at check (app.js:23) at HTMLElement.onclick (index.html:30)

HTML code:

<body>
   <div class="main">
      <input id="item" type="text">
      <i id="addItem" class="fas fa-plus"></i><br> <br>
   </div>

   <div class="items">
      <div id="addedItems"></div>
   </div>
   <i onclick='deleteAll()' class="fas fa-trash"></i>
</body>

JS code:

const item = document.getElementById("item");
const add = document.getElementById("addItem");
const addedItems = document.getElementById("addedItems");

add.onclick = function() {
   const itemValue = item.value;
   const itemValue2 = ' ';

   if (itemValue && itemValue2) {
       localStorage.setItem(itemValue, itemValue2);
       location.reload();
   }
};

for (let a = 0; a < localStorage.length; a++) {
   const itemValue = localStorage.key(a);
   addedItems.innerHTML += `<div class= 'text'> <div>${itemValue}</div> <div> 
                            <i onclick='check("${itemValue}")' class="fas fa-check"></i><i 
                            onclick='deleteItem("${itemValue}")' class="fas fa-trash-alt"></i> 
                            </div></div> <br>`;

}

function check(itemValue) {
   itemValue.classList.add("mystyle");
}

function deleteItem(itemValue) {
  localStorage.removeItem(itemValue);
  location.reload();
}

function deleteAll() {
   localStorage.clear();
   location.reload();
} 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try this answer:

  <script>
    const item = document.getElementById("item");
const add = document.getElementById("addItem");
const addedItems = document.getElementById("addedItems");

add.onclick = function() {
const itemValue = item.value;
const itemValue2 = ' ';

if (itemValue && itemValue2) {
  localStorage.setItem(itemValue, itemValue2);
  location.reload();
}
};

for (let a = 0; a < localStorage.length; a++) {
const itemValue = localStorage.key(a);
addedItems.innerHTML += `<div class= 'text'> <div>${itemValue}</div> <div> 
                          <i onclick='check("${itemValue}", ${a})' class="fas fa-check" id="${a}"></i><i 
                          onclick='deleteItem("${itemValue}")' class="fas fa-trash-alt"></i> 
                          </div></div> <br>`;

}

function check(itemValue, id) {
const elem = document.getElementById(id);
elem.classList.add("mystyle");
}

function deleteItem(itemValue) {
localStorage.removeItem(itemValue);
location.reload();
}

function deleteAll() {
localStorage.clear();
location.reload();
}
  </script>
  <div class="main">
    <input id="item" type="text">
    <i id="addItem" class="fas fa-plus"></i><br> <br>
  </div>

  <div class="items">
    <div id="addedItems"></div>
  </div>
  <i onclick='deleteAll()' class="fas fa-trash"></i>

Only changes I made is pass id of element for checkbox click along with itemValue element. Value you are passing checkbox value not document element. If you console you will get to know itemValue is just string not DOM element.

Here is fiddle : https://jsfiddle.net/aviboy2006/ortqu1ps/

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