Lesson 13 of 28

In progress

Course progress

JavaScript

Project: a filterable task panel

Combine selection, rendering, events and empty states in one small panel that stays usable with a keyboard.

This project is deliberately small: an input, a list and a count. It is enough to show the shape of every interactive interface, and small enough to read in one sitting.

What you will learn

  • Keep one state object and render the list from it.
  • Delegate events so re-rendering does not lose handlers.
  • Announce counts and show an empty state instead of a blank list.

What you will build

  • A text field that filters tasks as you type.
  • A list rendered from data, not hand-written HTML.
  • An empty state when nothing matches.
  • A live count that is announced to assistive technology.
  • A button per task that toggles completion.

Data first, DOM second

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

The array is the source of truth. The DOM is a rendering of it. When the two disagree, bugs appear: a row disappears but the count does not change, or a checkbox shows the wrong state.

render reads the data and rebuilds the list. Every interaction changes the data, then calls render.

The markup

<section aria-labelledby="panel-title">
<h2 id="panel-title">Tasks</h2>
<label for="filter">Filter tasks</label>
<input id="filter" type="search" placeholder="Type to filter">
<p id="count" role="status"></p>
<ul id="list"></ul>
<p id="empty" hidden>No tasks match that filter.</p>
</section>

The role="status" paragraph announces count changes without moving focus. The empty state is present in the DOM and hidden, so the script only toggles the hidden attribute.

Rendering

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

replaceChildren() empties the list in one call and is faster than a loop of removeChild. Rebuilding the whole list is fine for tens of items; for thousands, update only changed rows.

Creating elements and setting textContent keeps titles safe. dataset.id stores the identity on the button so a single delegated handler can find the task.

Wiring the events

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

The filter uses input so results update as the person types. The list uses one delegated listener, which keeps working after every re-render, because the listener is on the list itself, not on the buttons.

The empty state

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

Call it at the end of render. Toggling hidden removes the element from the accessibility tree as well as the page, which is the correct behaviour for a message that does not apply.

Keyboard and screen reader check

  1. Tab to the filter and type. Results change without losing focus.
  2. Tab to a task’s button and press Enter or Space. It toggles.
  3. Listen for the count: role="status" announces the new value.
  4. Check the empty state: it appears as text, not as a silent blank area.

If step 2 fails, the "button" is probably a div or a link without an href. Use a real button.

Storing the filter text in the DOM only, and re-reading it from the input inside every function, makes rendering depend on a hidden external state. Keep one state object and render from it. The input updates the state; it is not the state.

Add a "Clear filter" button that empties the input and resets the state, and make it hidden when the filter is already empty. Keep the count announcement working.

Show solution
JavaScript
Editable. Runs in your browser.





Press Run to see the output.

In the real page, attach a click handler that calls clearFilter, then render(). Set the button’s hidden property from state.filter.length === 0 inside render, so visibility follows the same source of truth as the list. Do not remove the input’s value without also resetting the state, or the next render will filter by stale text.

Extending it

The same shape supports search results, a product grid, a settings list or a comment thread:

  1. Keep data in one place.
  2. Render from data with textContent and created elements.
  3. Delegate events from a stable container.
  4. Update data, then re-render.
  5. Announce changes that happen without focus moving.

That is a small model of how larger frameworks work, and knowing it makes their abstractions easier to judge.

Recap

  • Keep one state object and render the DOM from it.
  • Create nodes and set text instead of concatenating HTML.
  • Delegate clicks, announce counts, and give empty results a visible message.

You have finished the DOM module. Next, you will fetch data from a server and handle the time it takes.

Why rebuild the whole list instead of updating one row?
Rebuilding is simpler and correct at small sizes. Optimise when a measurement shows it matters, and then update individual rows.
Is replaceChildren supported?
Yes in current browsers. For very old ones, set `innerHTML = “”` or remove children in a loop.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.