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

138
Views
To-do list JavaScript

I'm trying to make to-do list but i'm stuck with local storage. Nothing appears when the page is reloaded. I assume it's about changeStorage() method but i don't fully understand what i'm doing wrong.

function addTask() {
    let addTaskButton = document.getElementById('add-task-button')
    let list = document.getElementById('task-list');
    let li = document.createElement('li');
    let checkbox = document.createElement('input');
    let taskText = document.createElement('span');
    let delButton = document.createElement('button');
    let btnText = document.createTextNode('Delete task');

    checkbox.addEventListener('change', changeStorage);
    checkbox.type = 'checkbox';
    checkbox.className = 'checkbox';
    taskText.innerText =  document.getElementById('input-task').value;
    taskText.className = 'task';
    delButton.className = 'delete-btn';
    delButton.onclick = deleteTask;
    addTaskButton.addEventListener('click', changeStorage);

    delButton.appendChild(btnText);
    li.appendChild(checkbox);
    li.appendChild(taskText);
    li.appendChild(delButton);
    list.appendChild(li);

    document.getElementById('input-task').value = '';
}

function changeStorage() {
    let tasks = document.getElementById('ul').innerHTML;
    localStorage.setItem('tasks', JSON.stringify(tasks));
}

function deleteTask () {
    this.parentNode.remove();
    changeStorage();
}

document.getElementById('add-task-button').addEventListener('click', addTask);

function loadList() {
    document.querySelector('ul').innerHTML = JSON.parse(localStorage.getItem("tasks")) || [];
}

window.addEventListener('load', loadList);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This should change what gets stored in localStorage into a valid JSON string, and then reconstruct LI elements from it when it's loaded.

function changeStorage() {
    let tasks = [...document.querySelectorAll("ul li")].map(t => t.innerHTML);
    localStorage.setItem('tasks', JSON.stringify(tasks));
}

function loadList() {
    document.querySelector('ul').innerHTML = (JSON.parse(localStorage.getItem("tasks")) || []).map(t => "<li>" + t + "</li>").join("");
}
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!