JavaScript Simplified · One concept, one day, one project · Day 16 of 32
Creating and Removing Elements
Building new pieces of a page from JavaScript, and taking them away. You turn a list of tasks into real items on the page.
By the end of today you can
- Create a new element with
createElement - Add it to the page with
append - Take an element off the page with
remove - Build a to-do list from data
Until now you changed elements that were already in the HTML. But a to-do list, a feed, a set of search results: these elements do not exist until the data arrives. Today JavaScript creates elements and adds them to the page.
1. Creating an Element
Three steps: create the element, give it some content, then add it to the page.
document.createElement("li")makes a new, empty list item (not on the page yet).- Set its
textContentto give it words. parent.append(newElement)puts it inside a parent that is on the page.
The first item came from HTML; the second was built and added by JavaScript. The page now shows both.
2. Building Many From Data
The real power is a loop: take an array of data and build one element for each item. This is how every list on the web is drawn.
An empty list in the HTML, an array of data, a loop that builds an element for each: that is the shape behind feeds, carts and search results everywhere.
3. Removing an Element
element.remove() takes an element off the page. Select it, then remove it.
Today’s Mini Project
📝 Dynamic To-Do List
Start from an array of tasks and build the whole list on the page with a loop. (On Day 19 you will add new tasks from a form; today JavaScript draws the starting list.)
After building the list, create one more li that says how many tasks there are (use tasks.length) and append it. Expected: a final line like "3 tasks in total".
Build a list of your three favourite foods from an array, using a loop.
Expected: three list items on the page, one per food, all created by JavaScript.
You have finished Layer 3. JavaScript can now read the page, change its content, change its look, and build new parts from data. Next week you make the page respond to the person using it: clicks, typing and forms.
Checkpoint
What are the three steps to add a new element?
Create it with createElement, fill it (for example set textContent), then add it to a parent on the page with append.
How do you draw a list from an array?
Loop over the array, and for each item create an element, set its text, and append it to a parent. That is how feeds and lists are built.
How do you take an element off the page?
Select it and call remove() on it, for example element.remove().
Tick Before You Move On
- ☐ I can create a new element with createElement
- ☐ I can add it to the page with append
- ☐ I can remove an element with remove
- ☐ I built the Dynamic To-Do List from data
Quick Recap
createElement makes a new element, not yet on the page. append adds an element inside a parent that is on the page. A loop over data builds one element per item: the pattern behind every list. remove takes an element off the page.
Next: Day 17, and a new layer: making the page respond to the user. You start with events, the moments your code can react to.