Lesson 6 of 8
A to-do list
- 15 min
- 4 checks
- 3 hints
- Go to task
Goal
Reading the input and creating elements
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.
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- Clicking "Add" creates an item in the list
- The item's text matches what was typed in the input
- The input is cleared after adding
- Empty text isn't added