A to-do app starts empty and grows as you add tasks. Where do those new list items come from?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
createElement makes a new element in memory; append puts it on the page.
const list = document.querySelector("#list");
const li = document.createElement("li");
li.textContent = "Walk the dog";
list.append(li);
console.log(list.children.length);
03Objective and prerequisites
Learning objective: Create elements with createElement, insert them with append, prepend, before and after, remove them, re-render lists from data, and clone templates.
A notes app keeps its notes in an array. Whenever a note is added or deleted, a render function rebuilds the list from the array with replaceChildren, so the page always matches the data.
// function render() {
// list.replaceChildren(...notes.map(noteElement));
// }
console.log("Data changes, then render() rebuilds the list");
12Knowledge check
1. Where is an element right after document.createElement("li")?
At the end of the body
Nowhere on the page yet
Inside the first ul
In the head
2. With <ul id="u"><li>A</li></ul>, what is printed?
const li = document.createElement("li");
li.textContent = "B";
document.querySelector("#u").prepend(li);
console.log(document.querySelector("#u").firstElementChild.textContent);
A
B
AB
An error
3. Why does the list grow by three items every time render() runs?
function render() {
for (const n of ["A", "B", "C"]) list.append(Object.assign(document.createElement("li"), { textContent: n }));
}
It appends without clearing the old items
map returns too many items
createElement duplicates
append copies elements
4. What does el.remove() do?
Deletes the variable
Takes the element out of the DOM
Clears its text only
Hides it with CSS
5. Why clone tpl.content instead of appending it?
Templates cannot be cloned
So the template can be reused for every item
To make it faster only
content is read-only text
6. Showing a list of user comments: which is safer?
list.innerHTML += <li>${c}</li> for each comment
Create li elements and set textContent
document.write each comment
Use eval to build HTML
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Add the missing item: Prints 3; Eggs is last
Render the team: Three items in order; No innerHTML
Re-render the to-do list: Prints 2; Done item text and class; Empty list clears
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-11-the-document-object-model/lesson-054-creating-and-removing-elements/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
createElement makes an element; append, prepend, before and after insert it.
remove() takes an element out of the page.
Render lists from data with replaceChildren to avoid duplicates.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationcreating and removing DOM elements
Context: I am learning creating and removing DOM elements in JavaScript.
Task: Explain createElement, append, remove and replaceChildren with a to-do list.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictioncreating and removing DOM elements drill
Context: I have practised creating and removing DOM elements.
Task: Give me six snippets that insert, move and remove elements and ask what the page shows.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingcreating and removing DOM elements bug
Context: My list shows every item twice after I add a new one.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationcreating and removing DOM elements practice
Context: I finished the creating and removing DOM elements exercises.
Task: Create three exercises that render lists from arrays.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewcreating and removing DOM elements review
Context: Here is my code that uses creating and removing DOM elements.
Task: Review my rendering code for duplicates, innerHTML with user data and unnecessary DOM writes.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.