Skip to the workspace
Pebellslearn by building
DOM APIs  ›  Lesson

Build an Interactive Blog List

DOM APIs 25 min

javascript / Build an Interactive Blog List

Explore each scene at your own pace.

01 · Build an Interactive Blog List

Build an Interactive Blog List

Build an Interactive Blog List

A browser interaction connects input events to state changes and renders the resulting state into the document. Keep the data model separate from DOM nodes so filtering, rendering and event handling have clear responsibilities.

The user changes a search value, the program chooses matching articles, and the browser displays the chosen records.

Build one complete interaction

The earlier lessons isolated selection, mutation and event listeners. This workshop combines them into a small blog list. It has a labelled search input, a result count, native buttons and a saved-state indicator. The article data is the model; the DOM is a view of that model.

Walk through the modelBuild an Interactive Blog ListAuthored snapshots of the example; use Run below to test the code.
query"mor"
saved IDsSet {}
The input event supplies a new query; no page reload is needed.
1 of 3
html

Typing dispatches an input event. Its handler reads the current value, updates query state and calls render. render selects records, creates elements, writes text with textContent, and replaces the list's children. An empty match is a valid state and is announced explicitly. This simple example rebuilds a short list; it is not a prescription for rendering thousands of items on every keystroke.

The query is trimmed and normalized for a deliberately case-insensitive substring match. It does not use a regular expression, so punctuation in user input is ordinary text. Keep labels associated with controls; placeholder text alone is not a label.

Trace a save click

The listener is registered on the list, not on each article button. This is event delegation: the click bubbles to the parent and the handler locates the closest button carrying data-id. Clicking a nested icon or label can make event.target a descendant of the button. closest finds the intended control; a direct equality test on event.target would miss those clicks. Capture the current element during the handler if you need it later—currentTarget is only meaningful for the current dispatch.

Saved IDs live in a Set outside the DOM. Clicking updates that Set and rerenders. Rebuilding the DOM does not reset the saved data because the data does not live in the discarded nodes. This demo intentionally keeps state only for the page's lifetime; it does not save into the learner's account or promise persistence after reload.

Avoid crossing trust boundaries through markup

Article titles are written with textContent, so a title containing angle brackets remains text. Inserting arbitrary API text through innerHTML would interpret markup and could create an injection risk. Native buttons provide keyboard activation; a clickable div does not automatically get the same behavior. In a real remote save flow, also represent pending and failed outcomes rather than declaring success before the server confirms it.

Build and verify

Change a title to <b>Not markup</b> and confirm literal text is displayed. Search for mixed-case text, then a nonexistent title, then clear the search. Save an article, filter it out and restore the list; its saved state should remain. Activate Save with the keyboard. The graded helper below checks filtering behavior only; the browser checklist checks actual DOM and event behavior. Neither is a claim of backend persistence.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.