Computer Programming 1 (JavaScript) • Problem Solving & Mini-Projects

Building List Manager — I can build a basic list manager in the DOM that adds/removes items at my level.

Learn to create, display, and manage a dynamic list of items using JavaScript and the Document Object Model.

A JavaScript data file with code brackets and a laptop.
Inside this lesson 10 sections 5 questions 4 figures

SECTION 1

Historical Context & Motivation

Before JavaScript existed, web pages were entirely static. If you wanted a user to add an item to a list, the page had to reload from the server every single time. This was slow, clunky, and felt nothing like the smooth apps we use today. The need for dynamic web pages — pages that could change their content without a full reload — drove the creation of JavaScript in 1995.

The Document Object Model (DOM) is the browser's internal representation of a web page as a tree of objects. JavaScript can read, add, change, and remove any part of that tree. This is the foundation of every interactive web application you have ever used — from social media feeds to to-do apps to shopping carts.

1995 • JavaScript Is Born

Brendan Eich created JavaScript in just ten days at Netscape, giving browsers the ability to run code and respond to user actions without contacting the server.

1998 • DOM Level 1 Standard

The W3C published the first DOM specification, establishing a common API so JavaScript could manipulate page elements the same way across different browsers.

2006 • jQuery Simplifies DOM Work

The jQuery library made DOM manipulation dramatically easier, popularizing the pattern of selecting elements and modifying them with short, readable code.

2013–2015 • Modern Frameworks Emerge

React, Angular, and Vue abstracted direct DOM manipulation into component-based architectures, but understanding the raw DOM remains essential for debugging and learning how those tools work under the hood.

The core question this lesson addresses is straightforward: how do you let a user type something, click a button, and see a new item appear on the page — then remove it with another click? Mastering this pattern unlocks the ability to build any interactive list-based feature on the web.

SECTION 2

Core Principles & Definitions

Building a list manager relies on a handful of core ideas that you will use again and again throughout your programming career. Each principle below connects JavaScript code to something the user sees on the screen.

1

DOM Selection

Before you can change an element, you must select it. Methods like document.getElementById() and document.querySelector() return a reference to an HTML element so your code can work with it.
2

Element Creation

Use document.createElement() to build a brand-new HTML element in memory. It does not appear on the page until you explicitly attach it to the DOM tree.
3

Appending & Removing

parentElement.appendChild(child) inserts the new element as the last child of a parent. element.remove() detaches it from the DOM entirely, making it disappear from the page.
4

Event Listeners

An event listener is a function that runs whenever a specific action occurs, such as a button click. You attach one with element.addEventListener('click', handler).
5

Reading Input Values

The .value property of an <input> element gives you whatever the user has typed. After reading it, you typically clear the field by setting .value = "".
✦

KEY TAKEAWAY

Think of the DOM like a bulletin board in your classroom. Selecting is pointing at a specific note on the board. Creating is writing a new note on a sticky pad. Appending is pinning that sticky note onto the board so everyone can see it. Removing is pulling a note off the board and tossing it in the recycling bin. The bulletin board is always the live page the user sees.

SECTION 3

Visual Explanation — The DOM Tree in Action

The diagram below shows the HTML structure of a simple list manager and how JavaScript interacts with the DOM tree when a user adds or removes an item. Follow the numbered arrows to trace the flow of data from the input field to the visible list.

List Manager DOM Flow<div id="app"><input id="itemInput"><button id="addBtn"><ul id="list"><li> Apples<li> Bananas① Read .valueconst text = input.value;const li = document.createElement('li');② Appendlist.appendChild(li);③ Removeli.remove();Figure 1 — Data flows from the input, through JavaScript, into the <ul> as a new <li>.
Arrow ① shows JavaScript reading the text from the input field. Arrow ② shows appendChild attaching a new <li> to the list. Arrow ③ shows remove() detaching an existing item from the DOM.

Notice that the entire process follows three steps every time the user clicks the "Add" button. First, JavaScript reads the text the user typed. Second, it creates a new <li> element and sets its text content. Third, it appends that element to the <ul> so it becomes visible on the page. Removal works in reverse: a click event on the item triggers remove(), and the element vanishes from both the DOM tree and the screen.

SECTION 4

How It Works — The Add/Remove Mechanism

Let's break the entire mechanism into two clear operations: adding an item and removing an item. Each operation follows a predictable sequence of DOM method calls.

The Add Operation

When the user clicks the "Add" button, the event listener fires and runs a function. Inside that function, the code reads the input's .value property to get the typed text. If the text is not empty, the code calls document.createElement('li') to create a new list item element in memory. It then assigns the text to the element using li.textContent = text. Finally, list.appendChild(li) places the element at the end of the <ul>, making it visible. The input field is then cleared.

The Remove Operation

Each new <li> gets its own "Delete" button. When that button is clicked, the event listener calls li.remove() on the parent <li>. This instantly detaches the element from the DOM tree, and the browser re-renders the page without it. No server request is needed — everything happens locally in the browser.

Complete Code Template

ℹ

Full JavaScript Code

Below is the complete JavaScript for a basic list manager. Study each line and match it to the flow diagram from Section 3.

// 1. Select existing elements const input = document.getElementById('itemInput'); const addBtn = document.getElementById('addBtn'); const list = document.getElementById('list'); // 2. Define the add-item handler function addItem() { const text = input.value.trim(); if (text === '') return; // guard against empty input const li = document.createElement('li'); li.textContent = text; // Create a delete button for this item const delBtn = document.createElement('button'); delBtn.textContent = 'Delete'; delBtn.addEventListener('click', function() { li.remove(); // 3. Remove operation }); li.appendChild(delBtn); list.appendChild(li); // 4. Append to the DOM input.value = ''; // 5. Clear the input field } // 6. Attach the handler to the button addBtn.addEventListener('click', addItem);

✦

KEY TAKEAWAY

The entire list manager revolves around a simple cycle: read → create → attach → (later) remove. Every interactive list feature on the internet — shopping carts, playlists, chat messages — follows this same fundamental pattern, just with more styling and data persistence layered on top.

SECTION 5

Anatomy of the HTML & CSS Structure

The JavaScript code needs a matching HTML page to work with. The diagram below shows the minimal HTML required and how CSS classes can style the list manager so it looks clean and professional.

HTML Structure & Browser PreviewHTML Source<!DOCTYPE html><html> <head> <title>List Manager</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="app"> <h1>My List</h1> <input id="itemInput" placeholder="Add item"> <button id="addBtn"> Add</button> <ul id="list"></ul> </div> <script src="app.js"> </script> </body></html>Browser PreviewMy ListAdd item...AddApplesDeleteBananasDeleteMilkDelete
Left panel: the minimal HTML source code. Color-coded lines match elements shown in the browser preview on the right. The <ul id="list"> (green) starts empty — JavaScript populates it dynamically.

A few things to notice in the HTML. The <script> tag is placed at the bottom of the <body> so that all the HTML elements exist in the DOM before the JavaScript tries to select them. If you put the script in the <head>, the elements haven't been created yet and getElementById would return null, causing errors.

💡

Pro Tip

Give every element you need to access from JavaScript a unique id attribute. This makes getElementById() straightforward and eliminates confusion when your project grows.

SECTION 6

Worked Example — Building a Grocery List Manager

Let's walk through building a grocery list manager from scratch. We will start with HTML, add the JavaScript, and trace what happens when the user types "Eggs" and clicks "Add."

Building a Grocery List Manager

  1. 1

    Step 1 — Write the HTML skeleton

    Create an HTML file with an <input id="itemInput">, a <button id="addBtn">Add</button>, and an empty <ul id="list"></ul>. Link a separate JavaScript file at the bottom of the body using a <script src="app.js"></script> tag.
    The page displays an input, a button, and an empty area for the list.
  2. 2

    Step 2 — Select elements in JavaScript

    In app.js, use document.getElementById() to grab references to the input, button, and list: const input = document.getElementById('itemInput'); and so on for each element.
    Three variables — input, addBtn, list — now point to live DOM elements.
  3. 3

    Step 3 — Write the addItem function

    Define a function called addItem. Inside it, read input.value.trim() and store it in a variable called text. The .trim() method removes extra spaces. If text is an empty string, return early so blank items cannot be added.
    The guard clause if (text === '') return; prevents empty list items.
  4. 4

    Step 4 — Create, configure, and append the <li>

    Call document.createElement('li') to create a list item. Set li.textContent = text. Create a delete button with document.createElement('button'), set its text to "Delete", and add a click listener that calls li.remove(). Append the delete button to the <li>, then append the <li> to the <ul>.
    "Eggs" appears at the bottom of the list with a "Delete" button beside it.
  5. 5

    Step 5 — Attach the listener and clear the input

    Call addBtn.addEventListener('click', addItem) to connect the button to the function. At the end of addItem, set input.value = '' so the input field resets, ready for the next entry.
    The complete list manager is now functional. Users can add and remove items dynamically.

SECTION 7

Strengths, Limitations & Common Mistakes

This basic list manager teaches essential DOM skills, but it also has clear limitations. Understanding both strengths and weaknesses helps you decide when this approach is sufficient and when you need something more powerful.

StrengthsLimitations
Simple and easy to understand — no libraries required.Data lives only in the DOM; refreshing the page erases everything.
Teaches the fundamental DOM API used by all frameworks.No data validation beyond checking for empty strings.
Instant feedback — changes appear immediately on screen.No way to edit an existing item without rebuilding it.
Works in every modern browser with zero setup.Performance may degrade with thousands of items because each one has its own event listener.

Comparison of strengths and limitations of a basic DOM list manager

Common Mistakes to Avoid

  • ✓ Forgetting to trim the input: Without .trim(), users can add items that are just spaces.
  • ✓ Placing the script in the <head>: The elements don't exist yet, so getElementById returns null.
  • ✓ Using innerHTML instead of textContent: Setting innerHTML with user input can create security vulnerabilities (cross-site scripting). Always use textContent for plain text.
  • ✓ Not clearing the input after adding: The user has to manually erase their text before typing a new item, which is a poor experience.
✦

KEY TAKEAWAY

A basic DOM list manager is like learning to ride a bicycle before driving a car. It teaches you balance, steering, and braking — the core mechanics. Frameworks like React and Vue are the cars: they add power and comfort, but you'll be a much better driver if you first master the bicycle.

SECTION 8

Connection to Advanced Concepts

The list manager you just built is a stepping stone to much more powerful techniques. As you progress in web development, you will encounter tools that automate and optimize the patterns you learned here. The table below compares the basic approach to more advanced alternatives.

FeatureBasic DOM (This Lesson)Advanced / Framework
Data storageItems exist only in the DOMData stored in a JavaScript array or database; DOM is generated from data
PersistenceLost on page refreshSaved to localStorage, a server, or a cloud database
Event handlingOne listener per delete buttonEvent delegation: one listener on the parent handles all children
Rendering approachManually create and append elementsDeclarative rendering via templates or virtual DOM (React, Vue)
Editing itemsNot supported without extra codeTwo-way data binding makes editing natural

Basic DOM approach vs. advanced framework approach

A great next project would be to add localStorage persistence to your list manager. With just a few lines of code, you can save the list as a JSON string in the browser's local storage and reload it when the page opens. This bridges the gap between a throwaway demo and a genuinely useful tool. Beyond that, learning event delegation — attaching one listener to the <ul> instead of one per <li> — will prepare you for efficient handling of hundreds or thousands of items.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

Explain in your own words why document.createElement('li') does not immediately make a new item appear on the page. What additional step is needed?
Show answer
Calling document.createElement('li') creates the element only in JavaScript's memory — it is not yet part of the DOM tree that the browser renders. To make it visible, you must attach it to an existing element in the tree using a method like parentElement.appendChild(li). Think of it as writing a note but not pinning it to the bulletin board yet. Until you pin it (append it), no one else can see it.

Question 2: BASIC

Write the single line of JavaScript that would select an HTML element with the id "taskList" and store it in a variable called myList.
Show answer
The correct line is: const myList = document.getElementById('taskList'); This uses the getElementById method to search the DOM for the element whose id attribute equals "taskList" and returns a reference to that element. The const keyword declares a variable that won't be reassigned.

Question 3: INTERMEDIATE

A student writes the following code but the delete button doesn't work. What is the bug? const li = document.createElement('li'); li.textContent = text; const delBtn = document.createElement('button'); delBtn.textContent = 'Delete'; delBtn.addEventListener('click', function() { delBtn.remove(); }); li.appendChild(delBtn); list.appendChild(li);
Show answer
The bug is on the line delBtn.remove();. This removes only the delete button itself, not the entire list item. The correct line should be li.remove(); so that the whole <li> element — including its text and its delete button — is removed from the DOM. This is a common mistake when the student confuses which element they intend to remove.

Question 4: APPLIED

You are building a playlist manager for a school music club. In addition to adding and removing songs, the club wants each song to display its track number (1, 2, 3, …) that updates automatically when a song is removed. Describe the approach you would take to keep the track numbers accurate. You do not need to write full code — pseudocode or a description of the logic is fine.
Show answer
After any add or remove action, you would loop through all the <li> elements still inside the <ul> and update each one's text to include its current position. You can use list.querySelectorAll('li') to get all remaining items, then iterate with a forEach loop, setting each item's number to its index + 1. This renumbering function should be called at the end of both the add and remove operations so the numbers stay synchronized with the actual list order.

Question 5: CRITICAL THINKING

Imagine your list manager has 500 items, each with its own delete button and event listener. A classmate suggests it would be more efficient to use a single event listener on the <ul> instead. Explain how this approach (called event delegation) would work, why it is more efficient, and what JavaScript property you would use inside the listener to determine which delete button was clicked.
Show answer
Event delegation works because of a DOM feature called event bubbling: when a child element is clicked, the event travels up through every ancestor in the tree. Instead of 500 individual listeners, you place one listener on the <ul>. Inside that listener, you check event.target to see which element was actually clicked. If event.target is a delete button (you might check its tag name or a CSS class), you navigate to its parent <li> using event.target.parentElement and call .remove() on it. This is more efficient because the browser manages one listener rather than 500, reducing memory usage and speeding up initial page load.

SUMMARY

Lesson Summary

Summary

In this lesson you learned how to build a fully functional list manager using core DOM manipulation techniques. The process begins with selecting elements using document.getElementById(), continues with creating new elements via document.createElement(), and attaches them to the visible page tree with appendChild(). Removing items is accomplished with element.remove(). User interaction is handled through event listeners attached via addEventListener().

Key best practices include using textContent instead of innerHTML for security, adding a guard clause to prevent empty items, placing the <script> tag at the bottom of the body, and clearing the input after each addition. This pattern — read → create → attach → remove — is the foundation of every interactive web application, and mastering it prepares you for frameworks like React, Vue, and Angular.

Varsity Tutors • Computer Programming 1 (JavaScript) • Building List Manager