Day 5: Events and Event Handlers
Introduction
In the world of web development, user interaction is the cornerstone of creating an engaging and responsive environment. This interaction is managed and facilitated by Events and Event Handlers. Let’s start our journey into the dynamic domain of JavaScript events and handlers.
Definition and Understanding of Events
In JavaScript, an event can be something the browser does, or something a user does. JavaScript allows us to execute code when events are detected. These are some examples of JavaScript events:
- A user clicking on an element
- A web page finishing loading
- A form being submitted
- A video being played, paused, or ending
- A user scrolling, moving the mouse, pressing a key on the keyboard
These actions often trigger code, allowing interactive and dynamic behavior in web applications. Each event corresponds to a particular element in the Document Object Model (DOM), such as a button, the document body, or the document itself.
Different Types of Events
Events come in a variety of types, each corresponding to a specific action or trigger. Let’s look at a few of the most commonly used events:
- click: This event occurs when a user presses and releases the left mouse button on an element.
- load: This event fires after the page has finished loading. It can be used to execute JavaScript code after the entire page and its resources (images, scripts, etc.) have been fully loaded.
- mouseover: This event is triggered when the mouse pointer moves onto an element. It’s often used to trigger interactive effects when a user hovers over an element with the mouse.
- keypress: This event occurs when a user presses a key on the keyboard. It’s widely used in form validations, games, and interactive elements that need to respond to user input.
This is just a small taste of the wide variety of events JavaScript provides. We’ll delve more into other types of events in subsequent sections.
Introduction to Event Handlers
An event handler is a function in JavaScript that runs when a specified event occurs. We attach this function (or handler) to an element using JavaScript’s event properties, allowing us to specify what happens when the event occurs.
For instance, consider a simple button on a webpage. We might want something to happen when this button is clicked – perhaps an image appears, or a piece of text changes. This is where event handlers come in. By setting the button’s onclick event to a specific function, we can run some code whenever the button is clicked.
Event handlers provide us with the power to create dynamic, interactive web pages that can respond to user input in a variety of ways. Whether it’s validating form data, creating a dropdown menu, or even developing a browser-based game, event handlers play a critical role in JavaScript development.
As we dive further into the topic, we will delve into the specifics of how these event handlers are set, how they can be removed, and more.
This brings us to the end of the introduction. We hope you now have a basic understanding of what events and event handlers are. As we delve deeper into the topic, we will cover more ground on these critical JavaScript concepts. Remember, hands-on practice is essential, so make sure you’re coding alongside the learning material. Happy coding!
Section 1: Understanding Events
1.1 Explanation of the Event Object
Just as real-world events have associated details (such as the time and location of a meeting), JavaScript events also have related details. For instance, when a mouse click event happens, you might want to know which button was clicked or where the click happened on the page. In JavaScript, this information is encapsulated in an Event object.
What is the Event Object and its Purpose
An Event object is a JavaScript object that contains information about an event. This object is automatically created by the browser when an event occurs and is passed as an argument to the event handler. This object has properties and methods which provide information about the event. For example, the ‘type’ property of the Event object can be used to find out which type of event was fired.
The purpose of the Event object is to provide detailed information about the event. This information can be used within the event handler function to control the flow of the program based on the specifics of the event.
Types of Event Objects
There are several types of Event objects corresponding to different types of events. For instance, there are MouseEvent, KeyboardEvent, and UIEvent objects for mouse, keyboard, and UI events respectively. Each of these Event objects has its own unique properties and methods in addition to those inherited from the base Event interface.
Properties of the Event Object
The properties of the Event object provide specific information about the details of an event. Here are a few commonly used ones:
- target: The target property gives the DOM element on which the event occurred or which the event was dispatched on.
- type: The type property returns a string that represents the event type (e.g., ‘click’, ‘keydown’).
- bubbles: This boolean property indicates whether the event bubbles up through the DOM or not.
- cancelable: Another boolean property, cancelable indicates whether the event can be cancelled.
Each event type may have its own specific properties. For example, a KeyboardEvent object has a ‘key’ property which gives the character pressed for the ‘keypress’ event.
Event Object Methods
The methods of the Event object allow you to manipulate the event in various ways. Some commonly used methods are:
- preventDefault(): This method cancels the event if it’s cancelable, meaning it stops the default action from occurring. For example, you can use it to stop a form from submitting.
- stopPropagation(): This method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed.
- stopImmediatePropagation(): Similar to stopPropagation(), but also prevents other handlers on the same element from being executed.
This provides a high-level overview of the Event object in JavaScript. As we progress through the course, we will delve deeper into each aspect, providing real-world examples and coding exercises to better understand these concepts.
1.2 Event Types
JavaScript provides a wide variety of event types that can respond to numerous user interactions and browser occurrences. The diversity of these event types allows developers to create highly interactive and user-friendly web pages. Let’s dive into some of these event types and examples of their use.
Click Events
A click event is fired when an element is clicked. The ‘click’ event is perhaps the most common event type, and it’s often used to create interactive elements, such as buttons or links.
Example:
document.getElementById("myButton").addEventListener("click", function() {
alert("You clicked the button!");
});
In this example, an alert is displayed when the button with the id ‘myButton’ is clicked.
Load Events
A load event is fired when a resource and its dependent resources have finished loading. The ‘load’ event is often used to execute JavaScript code after the entire page, including all dependent resources (images, scripts, etc.), have loaded.
Example:
window.addEventListener("load", function() {
console.log("All resources are loaded!");
});
In this example, a message is logged to the console when the window and all its resources are fully loaded.
Mouseover Events
A mouseover event is triggered when the mouse pointer moves onto an element. It’s often used to trigger interactive effects when a user hovers over an element with the mouse.
Example:
document.getElementById("myElement").addEventListener("mouseover", function() {
this.style.backgroundColor = "yellow";
});
In this example, the background color of an element with the id ‘myElement’ changes to yellow when the mouse pointer moves over it.
Keypress Events
A keypress event is triggered when a key is pressed and released. It’s useful for detecting user input to a form or implementing keyboard shortcuts.
Example:
document.addEventListener("keypress", function(e) {
console.log(`Key pressed: ${e.key}`);
});
In this example, the key that was pressed is logged to the console each time a key is pressed and released.
Exploring Other Lesser-Known Events
Beyond the above-mentioned event types, JavaScript also provides a variety of other, lesser-known events:
- contextmenu: This event is fired when the user triggers the context menu (typically by a right-click).
- resize: This event is fired when the document view has been resized.
- scroll: This event is fired when the document view or an element has been scrolled.
- change: This event is fired when the value of an input or select element is changed.
Each of these events can provide additional levels of interactivity to your web pages, enhancing the user experience. We will explore more of these event types in subsequent sections, complete with coding examples and exercises. Remember, the key to mastering events is practice. Don’t just read – code along!
Section 2: Event Handlers
2.1 Explanation of Event Handlers
Definition and Purpose of Event Handlers
Event handlers in JavaScript are functions that are assigned to a specific event for a particular DOM element. When the specified event is detected on the element, the event handler function is triggered.
The primary purpose of an event handler is to enable interactivity on a webpage. They help to create dynamic web pages by allowing code execution in response to user actions or browser events.
Inline Event Handlers
Inline event handlers are directly added within the HTML tag. The event handler function is written as an attribute within the tag. Here’s an example of an inline event handler:
<button onclick="alert('You clicked the button!')">Click Me!</button>
In this example, when the button is clicked, an alert is displayed with the message ‘You clicked the button!’.
While inline event handlers are easy to implement, they are generally considered a poor practice because they mix JavaScript code with the HTML markup. This approach makes code harder to read, debug, and maintain.
Event Handler Properties
Another way to assign an event handler is by using an HTML element’s event handler properties in JavaScript. In this method, the event handler is a property of the DOM object.
Here’s an example:
let button = document.getElementById('myButton');
button.onclick = function() {
alert('You clicked the button!');
};
In this example, the ‘onclick’ property of the button with the id ‘myButton’ is set to a function that shows an alert when the button is clicked.
Event Handler Methods
Event handler methods, such as addEventListener() and removeEventListener(), provide a more flexible way to handle events.
The addEventListener() method attaches an event handler to a specified element. It takes two arguments: the event to listen for, and the function to run when the event occurs.
Here’s an example of using addEventListener():
let button = document.getElementById('myButton');
button.addEventListener('click', function() {
alert('You clicked the button!');
});
The removeEventListener() method is used to remove an event handler that has been attached with the addEventListener() method.
let button = document.getElementById('myButton');
let handler = function() {
alert('You clicked the button!');
};
button.addEventListener('click', handler);
button.removeEventListener('click', handler);
In this example, the event listener is removed immediately after being added, so clicking the button will do nothing.
This concludes the basic explanation of event handlers. They are powerful tools that allow you to make your web pages interactive and responsive to user actions. Practice using event handlers in your code to fully understand their use and implications.
2.2 Adding and Removing Event Handlers
Event handlers can be added or removed from elements using specific methods provided by JavaScript. These methods allow developers to dynamically control when and how an element should respond to a specific event.
The addEventListener() Method
The addEventListener() method is used to add an event listener to a specified element. This method takes two primary arguments: the event to listen for and the function to be executed when the event occurs.
Here’s an example of how to use addEventListener():
let button = document.getElementById('myButton');
button.addEventListener('click', function() {
console.log('Button is clicked!');
});
In this example, a ‘click’ event listener is added to the button with the id ‘myButton’. When the button is clicked, ‘Button is clicked!’ is logged to the console.
The removeEventListener() Method
The removeEventListener() method is used to remove an event listener that has been previously added to an element using addEventListener(). It takes the same arguments as addEventListener(): the event type and the event handler function.
Here’s an example:
let button = document.getElementById('myButton');
let handler = function() {
console.log('Button is clicked!');
};
button.addEventListener('click', handler);
button.removeEventListener('click', handler);
In this example, the event listener is removed immediately after being added, so clicking the button will do nothing.
Using Anonymous Functions in Event Listeners
In the examples above, we have used anonymous functions as event handlers. An anonymous function is a function without a name. While anonymous functions are handy, they have a limitation when it comes to removing event listeners.
The removeEventListener() method requires the exact same function to be passed as was passed to addEventListener(). However, with anonymous functions, this is not possible because we cannot reference the exact anonymous function used in addEventListener().
Consider the following:
let button = document.getElementById('myButton');
button.addEventListener('click', function() {
console.log('Button is clicked!');
});
// This won't work!
button.removeEventListener('click', function() {
console.log('Button is clicked!');
});
Even though the functions look identical, they are two separate functions in memory. To use removeEventListener(), you will need to assign the function to a variable when you first declare it, then use that variable to add and remove the listener:
let button = document.getElementById('myButton');
let handler = function() {
console.log('Button is clicked!');
};
button.addEventListener('click', handler);
button.removeEventListener('click', handler);
This way, you can add and remove the exact same function, allowing the removeEventListener() method to work as expected.
Adding and removing event handlers dynamically gives us a great deal of control over the interactive elements of our webpage. Understanding how to properly use these methods is crucial in managing events in JavaScript. Be sure to practice with these methods to get a good grasp of how they work!
Section 3: Event Propagation
3.1 Explanation of Event Propagation
In JavaScript, when an event occurs, such as a click on a button, it’s not just that element that’s aware of the event. Other elements in the Document Object Model (DOM), like the parent, the parent’s parent, and so on, are also aware of the event. This process is called event propagation.
Event propagation in the DOM is divided into three phases:
- Capturing Phase
- Target Phase
- Bubbling Phase
Understanding Event Flow: Capture Phase, Target Phase, and Bubbling Phase
Capture Phase: The event starts from the topmost element in the DOM tree (usually the window object), and descends down to the target element, invoking any event listeners found along the way that were registered with the capture phase flag set to true.
Target Phase: This is where the event has reached its target element, and any event handlers on the target element itself are invoked.
Bubbling Phase: After reaching the target, the event then begins to bubble up from the target element to the topmost node in the DOM, invoking any event handlers found along the way. The event handlers are executed in the reverse order of the capturing phase.
It’s important to note that not all events bubble. You can check if an event bubbles by checking the bubbles property of the event object.
Differences Between Event Bubbling and Event Capturing
These are two models for event propagation in the DOM:
- Event Bubbling: In the bubbling model, the event is first fired for the innermost element (the actual element that was clicked) and then propagated to outer elements. This is the most commonly used model, and it’s the default in modern browsers.
- Event Capturing: In the capturing model, the event is first captured by the outermost element and propagated to the inner elements. This model is rarely used, but it can be enabled by setting the useCapture argument to
truein theaddEventListener()method.
Understanding event propagation is key to mastering events in JavaScript. It allows for the control of how an event is handled and provides a deep understanding of the event life cycle in the DOM.
3.2 Event Propagation Methods
JavaScript provides two methods, stopPropagation() and preventDefault(), that give us more control over the behavior of events. These methods are part of the Event object and can be invoked inside event handlers.
stopPropagation() Method
The stopPropagation() method is used to stop the propagation of an event in the event flow. This means that no other event listeners will be called, whether they’re on the same element or on its ancestors in the propagation chain.
Here’s an example:
document.getElementById("myButton").addEventListener("click", function(e) {
e.stopPropagation();
console.log('Button is clicked!');
});
In this example, if there were other click event listeners on the same button or on its parent elements, they would not be called.
It’s worth mentioning that stopPropagation() does not prevent the default action of the event. For example, if the event is a ‘click’ event on a link, the link will still be followed. If you want to prevent the default action, you need to use the preventDefault() method.
preventDefault() Method
The preventDefault() method is used to cancel the default action of the event. It does not stop the propagation of the event, it simply prevents the browser’s default action associated with the event.
Here’s an example:
document.querySelector("a").addEventListener("click", function(e) {
e.preventDefault();
console.log('Link was clicked, but will not be followed.');
});
In this example, if there’s a ‘click’ event on a link, the preventDefault() method prevents the link from being followed, but other ‘click’ event listeners (on the same element or its ancestors) will still be called.
Understanding these methods is crucial for managing the behavior of events. They give us the ability to control the propagation of an event in the DOM tree and to prevent unwanted default actions. Make sure to practice using these methods to understand their implications.
4.2 Implementing Event Delegation
Event delegation is a technique in JavaScript where you delegate the handling of events to a parent element rather than handling the events in the child elements themselves. This technique is particularly useful when dealing with dynamic content, where elements are frequently added or removed.
Let’s look at an example where we have a dynamic list, and we want to log the content of the list item when it’s clicked.
HTML:
<ul id="myList">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<button id="addButton">Add Item</button>
JavaScript:
// Adding event listener to the list
document.getElementById('myList').addEventListener('click', function(e) {
if(e.target && e.target.nodeName === "LI") {
console.log('List item clicked: ' + e.target.textContent);
}
});
// Function to add new list item
document.getElementById('addButton').addEventListener('click', function() {
let newListItem = document.createElement('li');
newListItem.textContent = 'Item ' + (document.getElementById('myList').childElementCount + 1);
document.getElementById('myList').appendChild(newListItem);
});
In this example, we add a click event listener to the parent ul element. When the list is clicked, we first check if the target of the event is a li element. If it is, we log the text content of the clicked list item.
We also add a click event listener to a button which adds a new li element to the ul when clicked.
Notice how we do not need to attach new event listeners when adding new li elements to the list. The click event on these new items will bubble up to the ul and trigger the existing event listener. This is the power of event delegation – it allows us to handle events on elements that may not even exist when the code is initially run.
This approach is not only convenient but also efficient. It can significantly improve performance when dealing with a large number of similar elements.
Section 5: Advanced Topics in Events
5.1 Custom Events
Custom events can be useful when you want to create more specific events than the standard ones provided by the browser, or when you want to encapsulate and communicate data across different parts of your application.
Creating and Dispatching Custom Events
You can create a custom event using the CustomEvent() constructor. This constructor takes two arguments:
- The name of the event (a string).
- An options object where you can set additional properties for the event, such as
bubbles,cancelable, anddetail.
Here’s an example of creating a custom event:
let myEvent = new CustomEvent('myCustomEvent', {
bubbles: true,
detail: { message: 'This is a custom event!' }
});
In this example, we’ve created a custom event named ‘myCustomEvent’. The event bubbles, and it carries additional data in the detail property.
After you’ve created the custom event, you can dispatch it (i.e., trigger it) using the dispatchEvent() method. This method is called on the element you want to dispatch the event from.
Here’s an example of dispatching the custom event:
let button = document.getElementById('myButton');
button.dispatchEvent(myEvent);
In this example, we’re dispatching the ‘myCustomEvent’ from the button with the id ‘myButton’.
Finally, you can listen for and respond to the custom event just like you would for a standard event:
button.addEventListener('myCustomEvent', function(e) {
console.log('A custom event has been triggered');
console.log('The message is: ' + e.detail.message);
});
In this example, when the ‘myCustomEvent’ is dispatched, we log a message and the detail data to the console.
Custom events are a powerful tool in JavaScript that provide a way to create and manage your own events. They can help you create more maintainable and modular code by allowing different parts of your application to communicate and react to specific events.
5.2 Keyboard and Form Events
Working with keyboard and form events can require some special considerations, as they involve user interactions that may be more complex or variable than simple clicks or mouse movements.
Keyboard Events
Keyboard events are fired when user interacts with the keyboard. There are three types of keyboard events: keydown, keypress, and keyup.
keydownevent is fired when a key is pressed down.keypressevent is fired when a key that produces a character value is pressed down.keyupevent is fired when a key is released.
Each of these events provides an event object that includes data about the key pressed, such as its character value (in event.key), its key code (in event.keyCode), and boolean values indicating whether modifier keys (Ctrl, Alt, Shift, etc.) were held down when the key was pressed.
One special consideration when working with keyboard events is accessibility. You should ensure that all functionality can be accessed using the keyboard alone, to support users who cannot use a mouse or prefer using the keyboard.
Form Events
Form events are fired in relation to user interactions with form elements. There are many types of form events, but some of the most commonly used include focus, blur, change, input, and submit.
focusevent is fired when an element has received focus.blurevent is fired when an element has lost focus.changeevent is fired when the value of an input, select, or textarea element has been changed and the element loses focus.inputevent is fired whenever the user has modified the data of the input.submitevent is fired when a form is submitted.
A special consideration when working with form events is validation. You might use the input or change events to perform real-time validation as the user fills in a form. When a form is submitted, you can prevent the form from actually being submitted by calling event.preventDefault() in the event handler, then perform final validation. If validation fails, you can display an error message to the user; if validation passes, you can then manually submit the form from your JavaScript code.
Working with keyboard and form events provides a way to create more interactive and responsive user experiences. By understanding how these events work, and the special considerations that come with them, you can create web applications that are more robust, user-friendly, and accessible.
5.3 Mobile Events
With the growing number of users accessing web content from mobile devices, understanding how to handle mobile-specific events is increasingly important. The primary differences between mobile and desktop events are related to touch interactions, which can provide a more nuanced set of user input data compared to mouse or keyboard events.
Understanding Touch Events
Touch events are typically fired when a user touches the screen, moves their finger while touching the screen, or removes their finger from the screen. The most commonly used touch events include touchstart, touchmove, touchend, and touchcancel.
touchstart: This event is fired when one or more fingers are placed on the touch surface.touchmove: This event is fired when one or more fingers move on the touch surface.touchend: This event is fired when one or more fingers are removed from the touch surface.touchcancel: This event is fired when a touch is interrupted in some way, such as by a modal alert or by the touch point leaving the browser viewport.
These touch events each provide a touches list in the event object, which contains information about all fingers currently on the screen. For each finger, you can access properties such as clientX, clientY, screenX, screenY, and others to get the location of the touch.
Other Mobile-Specific Events
Aside from the touch events, there are other events that are specific to mobile devices, including:
orientationchange: This event is fired when the orientation of the device changes, for example from portrait to landscape mode. The new orientation can be accessed through thewindow.orientationproperty.deviceorientation: This event is fired when the physical orientation of the device changes. The event object includes properties for the rotation in three dimensions:alpha(rotation around the z-axis),beta(rotation around the x-axis), andgamma(rotation around the y-axis).devicemotion: This event is fired when the acceleration or rotation rate of the device changes.
Working with mobile events provides the opportunity to create a user-friendly and interactive experience for mobile users. By understanding these events, you can design web applications that respond appropriately to a wider range of user interactions.
Conclusion
Over the course of this extensive material, we’ve delved deep into the world of JavaScript events and event handlers. We’ve defined what they are, explored different types of events, and learned how to set up event handlers to respond to these events.
Events are central to creating interactive and dynamic web experiences. From simple mouse clicks to complex form and touch interactions, events allow your JavaScript code to respond to user actions in real-time, creating a more engaging and responsive user experience.
In addition, we’ve covered more advanced topics, such as event propagation, event delegation, custom events, and mobile-specific events. Understanding these concepts is essential for more advanced JavaScript programming and can help you manage complex user interactions more effectively.
Remember, the best way to learn is by doing. Here are some additional resources and exercises for you to practice:
- MDN Web Docs: Mozilla’s MDN Web Docs is an excellent resource for detailed information and examples on JavaScript events and event handling. Check out their pages on DOM events and Event reference.
- JavaScript.info: This website provides a comprehensive tutorial on JavaScript and DOM manipulation, including a section on Events.
- W3Schools: Another great resource for learning JavaScript, with interactive examples and exercises. Visit their JavaScript HTML DOM Events page.
Finally, consider creating a small project where you can apply these concepts, such as a simple interactive game or a form with advanced validation. These practical applications will solidify your understanding and get you comfortable with using events and event handlers in JavaScript.
Thank you for your time and attention. Keep practicing and exploring, and soon handling events in JavaScript will become second nature to you!
Supplemental Materials
Here are some suggested supplemental materials to enhance your understanding of events and event handlers. These materials include quizzes, exercises, additional resources, and coding challenges.
1. Quiz
Check your understanding of events and event handlers with this short quiz:
- What is event bubbling in JavaScript?
- How do you add an event handler to a button element with the ID “myButton” to respond to click events?
- What is the purpose of the
preventDefault()method in an event object? - Explain what a custom event is and provide an example of when you might use one.
- What is event delegation, and why is it useful?
2. Additional Resources for Further Reading
- JavaScript Event KeyCodes: A website that allows you to find out the key code for any key or button on your keyboard.
- You Don’t Need jQuery: A website that provides examples of how to perform common tasks using vanilla JavaScript, including handling events.
- JavaScript30: A 30-day vanilla JavaScript coding challenge that includes several projects involving events and event handling.
3. Hands-On Exercises
- Create a webpage with a form that uses JavaScript to validate the form inputs in real-time as the user types. If the input is invalid, display an error message.
- Write a script that logs the x and y coordinates of the mouse whenever the mouse moves.
- Create a custom event that is dispatched when a certain condition is met (for example, when a counter reaches a certain number), and write an event listener that responds to this custom event.
4. Interactive Coding Challenges
- Codewars: Codewars provides a large number of coding challenges that you can filter by language (including JavaScript) and difficulty.
- HackerRank: HackerRank’s 10 Days of JavaScript challenge includes problems on events and event handling.
- LeetCode: LeetCode has a number of problems tagged ‘event’ that provide a chance to practice handling events in different scenarios.
Working through these materials will help you understand the concepts better and prepare you to use events and event handlers effectively in your own JavaScript code.
Supplemental Materials
Here are some suggested supplemental materials to enhance your understanding of events and event handlers. These materials include quizzes, exercises, additional resources, and coding challenges.
1. Quiz
Check your understanding of events and event handlers with this short quiz:
- What is event bubbling in JavaScript?
- How do you add an event handler to a button element with the ID “myButton” to respond to click events?
- What is the purpose of the
preventDefault()method in an event object? - Explain what a custom event is and provide an example of when you might use one.
- What is event delegation, and why is it useful?
2. Additional Resources for Further Reading
- JavaScript Event KeyCodes: A website that allows you to find out the key code for any key or button on your keyboard.
- You Don’t Need jQuery: A website that provides examples of how to perform common tasks using vanilla JavaScript, including handling events.
- JavaScript30: A 30-day vanilla JavaScript coding challenge that includes several projects involving events and event handling.
3. Hands-On Exercises
- Create a webpage with a form that uses JavaScript to validate the form inputs in real-time as the user types. If the input is invalid, display an error message.
- Write a script that logs the x and y coordinates of the mouse whenever the mouse moves.
- Create a custom event that is dispatched when a certain condition is met (for example, when a counter reaches a certain number), and write an event listener that responds to this custom event.
4. Interactive Coding Challenges
- Codewars: Codewars provides a large number of coding challenges that you can filter by language (including JavaScript) and difficulty.
- HackerRank: HackerRank’s 10 Days of JavaScript challenge includes problems on events and event handling.
- LeetCode: LeetCode has a number of problems tagged ‘event’ that provide a chance to practice handling events in different scenarios.
Working through these materials will help you understand the concepts better and prepare you to use events and event handlers effectively in your own JavaScript code.