CodingNic

Custom Modals

Save Changes from the Edit Modal

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

javascript
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:

javascript
if (!title) {
  return;
}

2. Update the selected task

Use the stored editing ID to find the original task and update only its editable fields:

javascript
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

javascript
saveTasks();
closeEditModal();
renderTasks();

Checkpoint: Saving from the modal updates the existing task instead of creating a new one.