Custom Modals
10 min read
Save Changes from the Edit Modal
Save Changes from the Edit Modal
Connect the modal’s Save changes button to the existing task data.
1. Read the modal values
const title = document.getElementById("modal-title-input").value.trim();
const due = document.getElementById("modal-due-input").value;
const priority = document.getElementById("modal-priority-input").value;
Do not save an empty title:
if (!title) {
return;
}
2. Update the selected task
Use the stored editing ID to find the original task and update only its editable fields:
const task = tasks.find((item) => item.id === editingTaskId);
if (!task) {
return;
}
task.title = title;
task.due = due;
task.priority = priority;
Keep the existing ID and completion state unchanged.
3. Save and close
saveTasks();
closeEditModal();
renderTasks();
Checkpoint: Saving from the modal updates the existing task instead of creating a new one.