|
5 | 5 | | [Introduction to Events in JavaScript](#introduction-to-events-in-javascript) | |
6 | 6 | | [Different Ways of Listening to Events](#different-ways-of-listening-to-events) | |
7 | 7 | | [Removing Event Listeners](#removing-event-listeners) | |
| 8 | +| [The Event Object](#the-event-object) | |
8 | 9 |
|
9 | 10 | ## [Introduction to Events in JavaScript](https://drive.google.com/uc?export=view&id=1tfi-wZ9BYL2wISnyZ2JCcutRPApHpyCV) |
10 | 11 |
|
@@ -207,4 +208,34 @@ Readings: |
207 | 208 |
|
208 | 209 | - [You’ve Got Options for Removing Event Listeners](https://www.macarthur.me/posts/options-for-removing-event-listeners) |
209 | 210 |
|
210 | | -- [How to remove an event Listener in JavaScript? ](https://linuxhint.com/remove-event-listener-javascript/) |
| 211 | +- [How to remove an event Listener in JavaScript? ](https://linuxhint.com/remove-event-listener-javascript/) |
| 212 | +
|
| 213 | +## The [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event) Object |
| 214 | +
|
| 215 | +An `Event` object is created whenever an event is triggered, such as a button click or a form submission. The `Event` object contains information about the event, such as the type of event, the target element that triggered the event, and any additional data associated with the event. |
| 216 | +
|
| 217 | +Here's an example of using the `Event` object to log information about a button click event: |
| 218 | +
|
| 219 | +```javascript |
| 220 | +// Get the button element |
| 221 | +const button = document.querySelector('button'); |
| 222 | + |
| 223 | +// Add a click event listener to the button |
| 224 | +button.addEventListener('click', function(event) { |
| 225 | + // Log information about the event |
| 226 | + console.log('Event type:', event.type); |
| 227 | + console.log('Target element:', event.target); |
| 228 | +}); |
| 229 | +``` |
| 230 | +
|
| 231 | +In this example, the `addEventListener()` method is used to add a click event listener to a button element. The second argument to the `addEventListener()` method is an anonymous function that takes an Event object as its argument. |
| 232 | +
|
| 233 | +Inside the anonymous function, information about the `Event` object is logged to the console. The `event.type` property is used to log the type of event (in this case, `'click'`), and the `event.target` property is used to log the target element that triggered the event (in this case, the button element). |
| 234 | +
|
| 235 | +By using the `Event` object, you can access a variety of properties and methods to help you handle events in JavaScript. For example, you can use the event.`preventDefault()` method to prevent the default behavior of an event (such as submitting a form), or you can use the `event.stopPropagation()` method to stop an event from propagating up the DOM tree. |
| 236 | +
|
| 237 | +Readings: |
| 238 | +
|
| 239 | +- [Javascript: let’s meet the event object](https://medium.com/launch-school/javascript-lets-talk-about-events-572ecce968d0) |
| 240 | +
|
| 241 | +- [JavaScript Event Objects Tutorial](https://www.nickmccullum.com/javascript/javascript-event-objects/) |
0 commit comments