Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!