Lesson 6 of 8

A to-do list

Goal

Reading the input and creating elements

01

Explanation

This lesson brings together everything so far: you read what the user typed, check it, then create a new element on the page.

You'll find the input's value in input.value. To create an element you write document.createElement('li'), then add it to the list with list.append(li).

Before adding anything, make sure the user actually typed something. value.trim() removes the spaces from both ends of the text, and if nothing is left, don't add anything. This is the simplest form of input validation, and it's what makes your app fit for real use instead of just a demo.

02

Your task

Make the button add the task to the list and then clear the input, and add nothing when the input is empty.

Checks

4 checks
  1. Clicking "Add" creates an item in the list
  2. The item's text matches what was typed in the input
  3. The input is cleared after adding
  4. Empty text isn't added
03

Hints

0/3
index.html
Preview
index.html100%
–/4

Press “Run and check” to grade your code.

4 checks listed under “Your task”.