Add lesson 6: Events and listeners
3 changed files with 36 additions and 0 deletions
lessons/06-events-and-listeners/README.md ADDED
| @@ -0,0 +1,19 @@ | ||
| 1 | +# Lesson 6: Events and listeners | |
| 2 | + | |
| 3 | +In this lesson you'll work through events and listeners step by step, with short exercises after each idea. | |
| 4 | + | |
| 5 | +Full lesson: [https://glibcode.net/courses/javascript-web/lessons/6](https://glibcode.net/courses/javascript-web/lessons/6) | |
| 6 | + | |
| 7 | +## Files | |
| 8 | + | |
| 9 | +- `events.js`: click and input listeners | |
| 10 | + | |
| 11 | +## Run it | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +npx serve . # then open http://localhost:3000 and the browser console | |
| 15 | +``` | |
| 16 | + | |
| 17 | +## Exercises | |
| 18 | + | |
| 19 | +See [exercises.md](exercises.md). Ask in the forum once you've tried. | |
lessons/06-events-and-listeners/events.js ADDED
| @@ -0,0 +1,12 @@ | ||
| 1 | +const button = document.querySelector("#like"); | |
| 2 | +let likes = 0; | |
| 3 | + | |
| 4 | +button.addEventListener("click", () => { | |
| 5 | + likes += 1; | |
| 6 | + button.textContent = `♥ ${likes}`; | |
| 7 | +}); | |
| 8 | + | |
| 9 | +const search = document.querySelector("#search"); | |
| 10 | +search.addEventListener("input", event => { | |
| 11 | + document.querySelector("#echo").textContent = event.target.value; | |
| 12 | +}); | |
lessons/06-events-and-listeners/exercises.md ADDED
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Exercises: lesson 6 | |
| 2 | + | |
| 3 | +1. Combine this lesson with the previous one: use something from both in a small program of your own. | |
| 4 | +2. Time yourself: rewrite the example from memory in 10 minutes, then diff it against the original. | |
| 5 | +3. Add a comment above each block explaining what it does, in your own words. | |