Practice and reinforce the concepts from Lesson 10
In this activity, you will:
addEventListener()
method⏱️ Estimated Time: 45-60 minutes total
:warning: Warning DO NOT DELETE the existing files in the template:
- Package files
- Any other files you didn't create
ONLY EDIT the necessary files.
In each exercise below, click the Stackblitz link and familiarize yourself with the interface.
⏱️ Time: 15 minutes
Follow these steps:
button
and select the button element using DOM.addEventListener()
method to add a click event to the button:
:bulb: Tip Use this syntax:
button.addEventListener("click", () => { alert("I love JavaScript!"); });
Expected Results:
When you click the button:
:bulb: Tip Quick Reference:
document.querySelector('.className')
or document.querySelector('#id')
() => { /*statement*/ }
⏱️ Time: 15 minutes
Goal: Create text that turns red when you hover over it
Follow these steps:
Add this paragraph element to your index.html
file:
<p class="m-5 p-2">I love JavaScript</p>
In your JavaScript file:
text
and select the paragraph elementmouseover
event listener with an arrow functionmouseout
event listener with an arrow function:bulb: Tip Helpful Methods:
text.classList.add("text-danger")
text.classList.remove("text-danger")
Expected Results:
Normal state:
When hovering:
⏱️ Time: 15 minutes
Goal: Create a key press detector that alerts which key was pressed
Follow these steps:
document
for keydown
eventsevent
parameterevent.key
:bulb: Tip Code Structure:
document.addEventListener("keydown", (event) => {
alert(event.key);
});
Expected Result:
When pressing the 'w' key:
Your completed projects must include:
Exercise 1 - Click Event:
addEventListener()
Exercise 2 - Mouse Events:
Exercise 3 - Keyboard Events:
:bulb: Tip Common Issues:
text-danger
class is included in your projectdocument
, not a specific elementwindow.addEventListener('DOMContentLoaded', () => { ... })
:information_source: Ready to submit? When you have completed all three Event exercises, submit your projects using the link below:
Before submitting, test that:
- :emoji: Button click shows alert
- :emoji: Text changes color on hover
- :emoji: Keyboard presses are detected