JavaScript DOM Event Listeners_ Comprehensive Guide
JavaScript DOM Event Listeners_ Comprehensive Guide
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
Removing an Event Listener 3
Event Object 3
Example: Access Event Properties 3
Event Propagation 4
Example: Bubbling vs. Capturing 4
Event Delegation 4
Example: Event Delegation 4
Prevent Default Behavior 5
Example: Prevent Default 5
Detailed Examples 5
Example 1: Toggle Class on Button Click 5
Example 2: Key Press Event 6
Exercises 6
Exercise 1: Button Counter 6
Solution: 6
Exercise 2: Dynamic List 6
Solution: 6
Exercise 3: Keyboard Navigation 7
Multiple-Choice Questions 7
Question 1: 7
Question 2: 7
Question 3: 8
Best Practices for Event Listeners 8
Event listeners allow JavaScript to detect and respond to user interactions or changes in the
DOM. This guide explains event listeners, their usage, examples, exercises, and quiz questions
to help you master this critical aspect of web development.
Event listeners are functions or handlers that wait for specific events, such as clicks, key
presses, or mouse movements, and execute code in response.
Common Events:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
Basic Syntax
element.addEventListener(event, callbackFunction);
function handleClick() {
alert("Button was clicked!");
}
const button = document.getElementById("myButton");
button.addEventListener("click", handleClick);
// Remove the event listener
button.removeEventListener("click", handleClick);
Event Object
The event object provides information about the event, such as the target element and mouse
position.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
});
Event Propagation
1. Event Bubbling: Events propagate from the target element to its ancestors.
2. Event Capturing: Events propagate from ancestors to the target element.
Event Delegation
Event delegation involves attaching a single event listener to a parent element and handling
child element events through the event.target property.
4
<script>
const list = document.getElementById("list");
list.addEventListener("click", (event) => {
if (event.target.tagName === "LI") {
alert(`You clicked ${event.target.textContent}`);
}
});
</script>
Some events, like form submissions or links, have default behaviors that can be prevented.
Detailed Examples
5
}
</style>
Exercises
Create a button that counts how many times it has been clicked and displays the count.
Solution:
<button id="counterButton">Click me</button>
<p id="count">Count: 0</p>
<script>
const button = document.getElementById("counterButton");
const countDisplay = document.getElementById("count");
let count = 0;
button.addEventListener("click", () => {
count++;
countDisplay.textContent = `Count: ${count}`;
});
</script>
Create an input field and a button. When the button is clicked, add the input value as a new list
item.
Solution:
<input type="text" id="itemInput" placeholder="Enter item" />
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
<button id="addItemButton">Add Item</button>
<ul id="itemList"></ul>
<script>
const input = document.getElementById("itemInput");
const button = document.getElementById("addItemButton");
const list = document.getElementById("itemList");
button.addEventListener("click", () => {
const newItem = document.createElement("li");
newItem.textContent = input.value;
list.appendChild(newItem);
input.value = ""; // Clear input
});
</script>
Create a box that moves up, down, left, or right based on arrow key presses.
Multiple-Choice Questions
Question 1:
1. addListener()
2. addEventListener()
3. onEvent()
4. listenEvent()
Answer: 2. addEventListener()
Question 2:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
7
Question 3:
1. event.currentTarget
2. event.sourceElement
3. event.target
4. event.listener
Answer: 3. event.target
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis