Showing data: lists, filters and live search
Turn an array of objects into a page with a render function, then add live search, a category filter and sorting, with an empty state and a result count, as real shopping and job sites do.
🎯 By the end of this lesson you will be able to:
- Write a render function that draws an array of objects on the page
- Filter and sort data in response to search boxes and drop-downs
- Show result counts and a helpful empty state
Outline
What you'll learn and why it matters
Product lists, search results, job boards, chat messages: most of what apps show is data drawn on the page. The professional pattern is to keep the data in an array, and write one render function that draws it. When something changes, you work out the new list and render again. Frameworks like React (in the Full-Stack course) are built around exactly this idea.
A render function
const menu = [
{ name: "Masala chai", price: 40, category: "drinks" },
{ name: "Filter coffee", price: 50, category: "drinks" },
{ name: "Ginger lemon tea", price: 45, category: "drinks" },
{ name: "Banana bread", price: 60, category: "snacks" },
{ name: "Samosa", price: 25, category: "snacks" },
{ name: "Veg puff", price: 30, category: "snacks" },
];
const list = document.querySelector("#menu-list");
function render(items) {
list.replaceChildren();
for (const item of items) {
const li = document.createElement("li");
li.textContent = `${item.name}: ₹${item.price}`;
list.append(li);
}
}
render(menu);
console.log(list.children.length);Console (Chrome and Edge)
6
Show the HTML and CSS this script works with
HTML
<h2>Menu</h2>
<ul id="menu-list"></ul>CSS
ul { padding-left: 1.25rem; }
.empty { color: #475569; font-style: italic; }Live preview: try it
Console
Nothing logged yet.
list.replaceChildren() with nothing inside the brackets removes everything from the list first, so calling render again never duplicates items. The function draws whatever array it is given; that is the key to the rest of this lesson.
Live search
On every input event, filter the data and render the result. Compare lower-case versions so *CHAI*, *Chai* and *chai* all match:
const menu = [
{ name: "Masala chai", price: 40, category: "drinks" },
{ name: "Filter coffee", price: 50, category: "drinks" },
{ name: "Ginger lemon tea", price: 45, category: "drinks" },
{ name: "Banana bread", price: 60, category: "snacks" },
{ name: "Samosa", price: 25, category: "snacks" },
{ name: "Veg puff", price: 30, category: "snacks" },
];
const list = document.querySelector("#menu-list");
const search = document.querySelector("#search");
const count = document.querySelector("#count");
function render(items) {
list.replaceChildren();
for (const item of items) {
const li = document.createElement("li");
li.textContent = `${item.name}: ₹${item.price}`;
list.append(li);
}
count.textContent = `${items.length} of ${menu.length} items`;
}
search.addEventListener("input", () => {
const term = search.value.trim().toLowerCase();
const matches = menu.filter((item) => item.name.toLowerCase().includes(term));
render(matches);
});
render(menu);Show the HTML and CSS this script works with
HTML
<label for="search">Search the menu</label>
<input type="search" id="search">
<p id="count" aria-live="polite"></p>
<ul id="menu-list"></ul>CSS
ul { padding-left: 1.25rem; }
.empty { color: #475569; font-style: italic; }Live preview: try it
Console
Nothing logged yet.
Type *tea* or *b* in the preview. An empty search box matches everything, because every string includes the empty string "".
Combining search, filter and sort
With several controls, keep one update function that reads all of them, works out the list in steps, and renders once:
const menu = [
{ name: "Masala chai", price: 40, category: "drinks" },
{ name: "Filter coffee", price: 50, category: "drinks" },
{ name: "Ginger lemon tea", price: 45, category: "drinks" },
{ name: "Banana bread", price: 60, category: "snacks" },
{ name: "Samosa", price: 25, category: "snacks" },
{ name: "Veg puff", price: 30, category: "snacks" },
];
const list = document.querySelector("#menu-list");
const count = document.querySelector("#count");
const controls = document.querySelector("#controls");
function render(items) {
list.replaceChildren();
for (const item of items) {
const li = document.createElement("li");
li.textContent = `${item.name}: ₹${item.price}`;
list.append(li);
}
count.textContent = items.length === 1 ? "1 item" : `${items.length} items`;
if (items.length === 0) {
const li = document.createElement("li");
li.className = "empty";
li.textContent = "Nothing matches. Try another search or category.";
list.append(li);
}
}
function update() {
const term = document.querySelector("#search").value.trim().toLowerCase();
const category = document.querySelector("#category").value;
const sort = document.querySelector("#sort").value;
let items = menu.filter((item) => item.name.toLowerCase().includes(term));
if (category !== "all") {
items = items.filter((item) => item.category === category);
}
if (sort === "low") {
items = [...items].sort((a, b) => a.price - b.price);
} else if (sort === "high") {
items = [...items].sort((a, b) => b.price - a.price);
}
render(items);
}
controls.addEventListener("input", update);
update();Show the HTML and CSS this script works with
HTML
<div id="controls">
<label for="search">Search</label>
<input type="search" id="search">
<label for="category">Category</label>
<select id="category">
<option value="all">All</option>
<option value="drinks">Drinks</option>
<option value="snacks">Snacks</option>
</select>
<label for="sort">Sort</label>
<select id="sort">
<option value="menu">Menu order</option>
<option value="low">Price: low to high</option>
<option value="high">Price: high to low</option>
</select>
</div>
<p id="count" aria-live="polite"></p>
<ul id="menu-list"></ul>CSS
ul { padding-left: 1.25rem; }
.empty { color: #475569; font-style: italic; }Live preview: try it
Console
Nothing logged yet.
| Step | Why |
|---|---|
Read every control inside update | The list always matches what the controls show right now |
| Filter, then filter, then sort | Each step narrows or reorders the result of the previous one |
Sort a copy [...items] | The original menu order is kept for *Menu order* |
| Render once at the end | Simple and fast; the page is drawn from one final list |
| Show a count and an empty message | People know the search worked, even when nothing matched |
Practice
Guided exercise
Job board filter
Render the jobs as Title (City). Add a checkbox #remote that, when ticked, shows only remote jobs, and show the count in #count.
- Write
render(items)that clears#jobsand adds anliper job. - Write
update()that readsdocument.querySelector("#remote").checked. - If ticked, filter to
job.remote === true. - Listen for
changeon the checkbox, and callupdate()once at the start.
Show the solution
const jobs = [
{ title: "Junior web developer", city: "Pune", remote: false },
{ title: "Front-end intern", city: "Accra", remote: true },
{ title: "QA tester", city: "Nairobi", remote: true },
];
const list = document.querySelector("#jobs");
const count = document.querySelector("#count");
const remote = document.querySelector("#remote");
function render(items) {
list.replaceChildren();
for (const job of items) {
const li = document.createElement("li");
li.textContent = `${job.title} (${job.city})`;
list.append(li);
}
count.textContent = `${items.length} jobs`;
}
function update() {
const items = remote.checked ? jobs.filter((job) => job.remote) : jobs;
render(items);
}
remote.addEventListener("change", update);
update();Show the HTML this script works with
<input type="checkbox" id="remote">
<label for="remote">Remote only</label>
<p id="count" aria-live="polite"></p>
<ul id="jobs"></ul>Live preview: try it
Console
Nothing logged yet.
How to get there: job.remote is already a boolean, so filter((job) => job.remote) keeps the remote ones. The ternary picks the filtered list or the full list, and render draws whichever it gets.
Your turn
Price limit
Add a number box #max to the menu example. Show only items costing at most that price; if the box is empty, show everything. Show the count in #count.
Show the solution
const menu = [
{ name: "Masala chai", price: 40, category: "drinks" },
{ name: "Filter coffee", price: 50, category: "drinks" },
{ name: "Ginger lemon tea", price: 45, category: "drinks" },
{ name: "Banana bread", price: 60, category: "snacks" },
{ name: "Samosa", price: 25, category: "snacks" },
{ name: "Veg puff", price: 30, category: "snacks" },
];
const list = document.querySelector("#menu-list");
const max = document.querySelector("#max");
const count = document.querySelector("#count");
function render(items) {
list.replaceChildren();
for (const item of items) {
const li = document.createElement("li");
li.textContent = `${item.name}: ₹${item.price}`;
list.append(li);
}
count.textContent = `${items.length} items`;
}
function update() {
const limit = Number(max.value);
const items = max.value === "" ? menu : menu.filter((item) => item.price <= limit);
render(items);
}
max.addEventListener("input", update);
update();Show the HTML this script works with
<label for="max">Maximum price (₹)</label>
<input type="number" id="max" min="0">
<p id="count" aria-live="polite"></p>
<ul id="menu-list"></ul>Live preview: try it
Console
Nothing logged yet.
How to get there: The empty-box check comes first, because Number("") is 0, which would hide everything. With 40, Masala chai (40), Samosa (25) and Veg puff (30) remain.
Common mistakes
Not clearing the list before rendering
You see: Items appear twice, then three times…
Fix: Start render with list.replaceChildren().
Changing the original data array when filtering or sorting
You see: Items disappear for good, or *Menu order* stops working
Fix: filter returns a new array; sort a copy with [...items].
Case-sensitive search
You see: *chai* does not find *Masala Chai*
Fix: Lower-case both sides before includes.
Separate listeners that each filter differently
You see: Choosing a category forgets the search text
Fix: Use one update function that reads every control.
Recap
- Keep data in an array and draw it with one render function that clears and rebuilds the list.
- Live search: filter on input, comparing lower-case text with includes.
- One update function reads every control, filters, sorts a copy, then renders once.
- Show a result count (aria-live) and a friendly empty state.
Key terms
- Render
- To draw data on the page as HTML elements.
- Empty state
- What a list shows when there is nothing to display, ideally with a hint about what to do.
- replaceChildren
- A DOM method; called with no arguments it removes all of an element's children.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Why does render start with `list.replaceChildren()`?
2. How do you make a search find "Chai" when someone types "chai"?
3. Why sort `[...items]` instead of `items`?
4. A category filter forgets the search text whenever it changes. What is the best fix?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.