Step 5
25 min
Generate the tasks' HTML from the data with createElement.
Definition
Rendering is the operation that turns an application's data into elements visible on the page. Here, it means converting the tasks array into a list of HTML elements displayed on screen.
To achieve this, JavaScript can create elements on the fly with document.createElement, then insert them into the DOM, that is, into the representation of the page that the browser keeps in memory. As soon as an element is inserted there, it appears on screen.
You have an array of tasks; now you need to turn it into HTML elements. You will write two functions: createTask, which builds the HTML of one task, and render, which redraws the whole list.
This is the step where your application finally starts to show something from its data. Until now, the tasks array only existed in memory, invisible to the user. By the end of this step, every object it contains will turn into a row on the page.
You can't write the <li> elements in advance in your HTML file: you don't know how many tasks the user will create, or what text they will type. The list must therefore be built dynamically, from the data.
The principle is always the same: the data is the reference, and the display is only its reflection. When the data changes, we clear the list and rebuild it entirely. It is simple, reliable, and avoids oversights: you never risk having a page that no longer matches the array.
Add this function after your existing code. It receives a task object and returns an <li> element ready to be inserted into the page. It builds exactly the same structure as the test <li> from step 3.
function createTask(task) {
const li = document.createElement("li");
li.className = "task";
li.dataset.id = task.id;
if (task.done) {
li.classList.add("done");
}
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = task.done;
checkbox.setAttribute("aria-label", "Mark as completed");
const text = document.createElement("span");
text.className = "task__text";
text.textContent = task.text;
const deleteButton = document.createElement("button");
deleteButton.type = "button";
deleteButton.className = "task__delete";
deleteButton.textContent = "×";
deleteButton.setAttribute("aria-label", "Delete task");
li.append(checkbox, text, deleteButton);
return li;
}document.createElement creates an element in memory, without displaying it yet.li.dataset.id = task.id writes a data-id attribute on the <li>: this is how we will find the relevant task when the user clicks (step 7).classList.add("done") applies the strikethrough style only if the task is completed.textContent inserts the text as is. If the user types HTML such as <script>, it is displayed as plain text and not executed: this is an essential protection against code injection.li.append(...) places the checkbox, the text and the button inside the <li> in a single statement.If you look closely, each element is built in four identical steps:
createElement;className, type, textContent;setAttribute, such as aria-label, which helps screen readers describe buttons that have no text;Also note that createTask does not touch the page: it simply returns an element. It is the render function that will take care of inserting it, which keeps each function simple and dedicated to a single job.
Now add the function that redraws the whole list, along with a first call to run it when the page loads:
function render() {
list.replaceChildren();
tasks.forEach((task) => {
list.appendChild(createTask(task));
});
emptyMessage.hidden = tasks.length > 0;
}
render();list.replaceChildren() empties the list (called with no arguments, it removes all children). This avoids duplicates every time the list is rendered again.forEach loops over the array and adds one <li> per task with appendChild.emptyMessage.hidden is true (message hidden) if there is at least one task, and false (message visible) otherwise.render() always starts again from the data, it always gives a consistent result, whatever the previous state of the page.Redrawing the whole list on every change may seem wasteful, but for an application of this size it is instant and much simpler to maintain than updating each element one by one. You only have a single function to call after any change: adding, deleting, checking a box or changing the filter.
Note the render(); call at the very bottom of the file: it triggers a first rendering as soon as the page loads. Without it, the page would stay empty until you called the function yourself. Later, when tasks are saved in the browser, this first call will automatically bring back the user's list on every visit.
Finally, order matters: createTask must exist before render is called. So write the two functions in that order, and place the render(); call after them.
There is another way to display content: the innerHTML property, which interprets text as HTML code. It is tempting, because you can write a whole task row in a single string. But it comes with a serious risk: if the user types a tag, the browser treats it as real HTML.
With textContent, everything the user writes stays plain text. That is why we build the elements one by one here: the code is a bit longer, but it is safe and easy to read. Remember this rule: whenever data comes from the user, insert it with textContent.
Reload the page: the message "No tasks to display" should appear, and the two test <li> elements from step 3 disappear (because render() empties the list). You can now delete this test code from your HTML: the line goes back to <ul id="list" class="list"></ul>.
Then test your function by adding tasks by hand from the console:
tasks.push({ id: 1, text: "Learn the DOM", done: false });
tasks.push({ id: 2, text: "Create my to-do list", done: true });
render();Two tasks should appear, the second one struck through. They will disappear when the page is reloaded: this is normal, saving comes in step 9.

return li, createTask returns undefined and appendChild throws an error.replaceChildren(), every call adds all the tasks again and creates duplicates.createTask builds one task; render redraws the whole list.render() again to update the page.textContent to insert the user's text safely.In the next step, you will wire up the form so that the user can add new tasks themselves.
Your turn
To really understand the role of render:
tasks[0].done = true. The page doesn't change: why?render(): the page updates. Remember this principle, we will use it after every change to the data.tasks.push({ id: 3, text: "<b>test</b>", done: false }), then call render(): the tags are displayed as text thanks to textContent.tasks.length = 0, call render() again and watch the "No tasks to display" message come back.