In this lesson, you will learn how to build a Study Task Manager using JavaScript and browser storage. This Study Task Manager will help you organize your study tasks efficiently, making it easier to manage your time and resources.
Browser storage allows websites to store data on your device. It is useful for saving information like to-do lists. For example, you can create a task planner that saves your tasks even after closing the browser. Therefore, this feature enhances user experience by keeping information accessible.
File: task_planner.js
// Task Planner Code
const taskInput = document.getElementById('taskInput');
const addButton = document.getElementById('addButton');
const taskList = document.getElementById('taskList');
// Load tasks from localStorage
let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
function displayTasks() {
taskList.innerHTML = '';
tasks.forEach((task, index) => {
const li = document.createElement('li');
li.textContent = task;
li.appendChild(createDeleteButton(index));
taskList.appendChild(li);
});
}
function createDeleteButton(index) {
const button = document.createElement('button');
button.textContent = 'Delete';
button.onclick = () => deleteTask(index);
return button;
}
function deleteTask(index) {
tasks.splice(index, 1);
saveTasks();
displayTasks();
}
addButton.onclick = () => {
const task = taskInput.value;
if (task) {
tasks.push(task);
taskInput.value = '';
saveTasks();
displayTasks();
}
};
function saveTasks() {
localStorage.setItem('tasks', JSON.stringify(tasks));
}
// Initial display
displayTasks();
Expected Output:
- A list of tasks displayed on the page.
- Each task has a delete button to remove it.
This code uses localStorage to save tasks. When you open the page, it loads the tasks stored and displays them. Each task has a delete button that removes it from the list and updates localStorage. As a result, your task list stays current and efficient.
Create your own task planner. Add features such as editing tasks or marking them as complete. Use the code above as a starting point. To start building your task organizer, try implementing additional functionalities like sorting tasks by due date or priority.
// Added edit and complete features
function editTask(index) {
const newTask = prompt('Edit task:', tasks[index]);
if (newTask) {
tasks[index] = newTask;
saveTasks();
displayTasks();
}
}
function markComplete(index) {
const li = taskList.children[index];
li.classList.toggle('completed');
}
saveTasks() after adding or deleting a task. This ensures that the changes are stored in localStorage.setItem to save data and getItem to load data from localStorage. This will prevent data loss.