To remove tasks from localStorage in your To-Do List application, you need to ensure that you update the stored tasks whenever a task is deleted. Here’s how to do it:
Step-by-Step Guide to Remove Tasks from localStorage
-
Identify the Task to Remove: When a user clicks the delete button for a task, you need to identify which task is being removed.
-
Update the localStorage: After removing the task from the displayed list, you should also update the
localStorageto reflect this change.
Updated JavaScript Code
Here’s how you can implement the removal of tasks from localStorage in your existing script.js file:
document.getElementById('addTaskButton').addEventListener('click', addTask);
document.addEventListener('DOMContentLoaded', loadTasks); // Load tasks on page load
function addTask() {
const taskInput = document.getElementById('taskInput');
const taskText = taskInput.value.trim();
if (taskText === '') {
alert('Please enter a task!');
return;
}
const taskList = document.getElementById('taskList');
const listItem = document.createElement('li');
listItem.textContent = taskText;
// Add a delete button to each task
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.style.marginLeft = '10px';
deleteButton.addEventListener('click', function() {
taskList.removeChild(listItem);
removeTaskFromStorage(taskText); // Remove from localStorage
});
listItem.appendChild(deleteButton);
taskList.appendChild(listItem);
taskInput.value = ''; // Clear the input field
saveTaskToStorage(taskText); // Save to localStorage
}
function saveTaskToStorage(task) {
let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
tasks.push(task);
localStorage.setItem('tasks', JSON.stringify(tasks));
}
function loadTasks() {
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
const taskList = document.getElementById('taskList');
tasks.forEach(task => {
const listItem = document.createElement('li');
listItem.textContent = task;
// Add a delete button to each task
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.style.marginLeft = '10px';
deleteButton.addEventListener('click', function() {
taskList.removeChild(listItem);
removeTaskFromStorage(task); // Remove from localStorage
});
listItem.appendChild(deleteButton);
taskList.appendChild(listItem);
});
}
function removeTaskFromStorage(task) {
let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
tasks = tasks.filter(t => t !== task); // Filter out the task to remove
localStorage.setItem('tasks', JSON.stringify(tasks)); // Update localStorage
}
Explanation of the Removal Process:
-
Delete Button Event Listener: When the delete button is clicked, it triggers the
removeTaskFromStoragefunction, passing the task text as an argument. -
Filtering Tasks: The
removeTaskFromStoragefunction retrieves the current list of tasks fromlocalStorage, filters out the task that needs to be removed, and updates thelocalStoragewith the new array of tasks. -
Updating localStorage: The updated array of tasks is then saved back to
localStorage, ensuring that the removed task is no longer stored.
Running the Updated Application:
- Save the updated
script.jsfile. - Open
index.htmlin a web browser. - You can now add tasks, and when you delete a task, it will be removed from both the displayed list and
localStorage.
This ensures that your To-Do List application accurately reflects the current state of tasks in both the UI and the stored data!
