Practice listening for and handling JavaScript DOM Events
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
stoplight-event-exercise/README.md

55 lines
2.2 KiB

# Stoplight Exercise
As always, fork and clone this repository.
![](stoplight.gif)
## Part 1
9 years ago
Add DOM event listeners and handlers to toggle the on/off state of three bulbs.
9 years ago
- When a user clicks on the "Stop" button, toggle the [stop bulb's color][stop-color].
- When a user clicks on the "Slow" button, toggle the [slow bulb's color][slow-color].
- When a user clicks on the "Go" button, toggle the [go bulb's color][go-color].
9 years ago
**TIP**: All three bulbs can be on/off independently of one another.
9 years ago
### Resources
9 years ago
- [MDN - `Document.querySelector()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector)
- [MDN - `EventTarget.addEventListener()`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener)
- [MDN - `click`](https://developer.mozilla.org/en-US/docs/Web/Events/click)
- [MDN - `Element.classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList)
9 years ago
## Part 2
9 years ago
Add new DOM event listeners and handlers to log the mouse state of each button.
9 years ago
- When a user's mouse enters a button, log `"Entered <textContent> button"` to the console.
- When a user's mouse leaves a button, log `"Left <textContent> button"` to the console.
9 years ago
**TIP:** Each event type will need a separate event listener.
9 years ago
### Resources
- [MDN - `mouseenter`](https://developer.mozilla.org/en-US/docs/Web/Events/mouseenter)
- [MDN - `mouseleave`](https://developer.mozilla.org/en-US/docs/Web/Events/mouseleave)
- [MDN - `Event.target`](https://developer.mozilla.org/en-US/docs/Web/API/Event/target)
## Bonus
Add **one** DOM event listener and **one** handler to log the state of each bulb.
- When a user clicks a button that just turned on, log`"<textContent> bulb on"` to the console.
- When a user clicks a button that just turned off, log`"<textContent> bulb off"` to the console.
**TIP:** A click on only a button should cause a message to be logged to the console.
### Resources
- [How JavaScript Event Delegation Works](https://davidwalsh.name/event-delegate)
8 years ago
[stop-color]: https://github.com/gSchool/stoplight-event-exercise/blob/master/index.css#L39
[slow-color]: https://github.com/gSchool/stoplight-event-exercise/blob/master/index.css#L43
[go-color]: https://github.com/gSchool/stoplight-event-exercise/blob/master/index.css#L47