Todo List in JavaScript
Manage tasks in JavaScript with an array, stable IDs, and a render function. Checkboxes complete tasks and buttons remove them; refreshing clears the list.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<form id="add"><label>Task <input id="task" required></label><button>Add</button></form><ul id="tasks"></ul>7<script>8const tasks = [];9let nextId = 1;10const list = document.querySelector('#tasks');11function render() {12 list.replaceChildren();13 for (const task of tasks) {14 const row = document.createElement('li');15 const toggle = document.createElement('input');16 toggle.type = 'checkbox'; toggle.checked = task.done;17 toggle.setAttribute('aria-label', 'Complete ' + task.text);18 toggle.onchange = () => { task.done = toggle.checked; render(); };19 const text = document.createElement('span');20 text.textContent = task.text;21 text.style.textDecoration = task.done ? 'line-through' : 'none';22 const remove = document.createElement('button'); remove.textContent = 'Remove';23 remove.onclick = () => { tasks.splice(tasks.findIndex(item => item.id === task.id), 1); render(); };24 row.append(toggle, text, remove); list.append(row);25 }26}27document.querySelector('#add').onsubmit = event => {28 event.preventDefault();29 const input = document.querySelector('#task');30 const text = input.value.trim();31 if (!text) return;32 tasks.push({ id: nextId++, text, done: false });33 input.value = ''; render(); input.focus();34};35</script>36</body>37</html>
Sample Output
Revise arrays appears unchecked
Adding creates an object with done false and renders a new list row.
The task is crossed out
Changing the checkbox updates done and rerenders the row.
The task disappears
Remove deletes that ID from the array before rebuilding the list.
In this one
- Model a task with an ID and completion state
- Add and remove array entries
- Rebuild the page from data
- Display typed text safely
How it works
- 1
Create an empty tasks array and a counter for stable IDs.
- 2
Handle form submission without reloading the page.
- 3
Trim and store the task, then clear the input.
- 4
Render each task with a checkbox and Remove button.
- 5
Tick a task to cross it out, then remove it to clear that row.
Edge cases
Spaces-only task
HTML required does not reject a string of spaces.
trim and the empty check reject it before it enters the tasks array.
Page refreshed
The tasks disappear in this memory-only version.
That is expected here. Browser storage needs explicit save, read, and JSON error handling; arrays are not saved automatically.
What's going on
A todo list stores tasks and lets the person add, complete, and remove them. This example turns a small array into a visible page, so a student can practise keeping data and the screen in agreement.
Each task has an ID, text, and a done value. The ID stays the same when the task changes. A counter creates different IDs even when tasks are added quickly. Submitting the form trims the text, rejects an empty task, and adds a new object to the array.
render rebuilds the list from the current data. A checkbox changes done; Remove deletes the task with that ID. The text is placed with textContent so a task cannot inject HTML into the page. This version stores tasks only in memory, so refreshing clears them. If you later add localStorage, save JSON with JSON.stringify, read it with JSON.parse inside try/catch, and check that the saved value is an array before using it.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does done store?
Pick an answer to check it.
Question 2
What prevents form submission from reloading the page?
Pick an answer to check it.
Questions people ask
Why use an ID instead of the text?
Two tasks can have the same text. An ID lets the Remove button target one specific task.
Why rebuild the list after a change?
The array is the source of truth. Rendering from it keeps the checkboxes and rows in the same state.
Can a task contain HTML tags?
Yes, as visible text. textContent does not execute those tags.