Skip to content

Commit a19aa8e

Browse files
committed
learn about Event object
1 parent 418cc3a commit a19aa8e

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

Working-with-Events/README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| [Introduction to Events in JavaScript](#introduction-to-events-in-javascript) |
66
| [Different Ways of Listening to Events](#different-ways-of-listening-to-events) |
77
| [Removing Event Listeners](#removing-event-listeners) |
8+
| [The Event Object](#the-event-object) |
89

910
## [Introduction to Events in JavaScript](https://drive.google.com/uc?export=view&id=1tfi-wZ9BYL2wISnyZ2JCcutRPApHpyCV)
1011

@@ -207,4 +208,34 @@ Readings:
207208
208209
- [You’ve Got Options for Removing Event Listeners](https://www.macarthur.me/posts/options-for-removing-event-listeners)
209210
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/)
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
const button = document.querySelector('button');
1+
const buttons = document.querySelectorAll('button');
22

33
/* Using the `on` property */
44

55
// button.onclick() = function() {
66

77
// }
88

9-
const buttonClickHandler = () => {
10-
alert('Button was clicked!');
9+
// const buttonClickHandler = () => {
10+
// alert('Button was clicked!');
11+
// }
12+
13+
const buttonClickHandler = event => {
14+
console.log(event);
15+
event.target.disabled=true;
1116
}
1217

1318
const anotherButtonClickHandler= () => {
@@ -19,9 +24,13 @@ const anotherButtonClickHandler= () => {
1924
// button.onclick = anotherButtonClickHandler;
2025

2126
// Recommended Approach
22-
button.addEventListener('click', anotherButtonClickHandler);
27+
// button.addEventListener('click', anotherButtonClickHandler);
2328

2429
// Suppose, we are removing event listener after 2 seconds
25-
setTimeout(() => {
26-
button.removeEventListener('click', anotherButtonClickHandler)
27-
}, 3000);
30+
// setTimeout(() => {
31+
// button.removeEventListener('click', anotherButtonClickHandler)
32+
// }, 3000);
33+
34+
buttons.forEach(btn => {
35+
btn.addEventListener('click', buttonClickHandler);
36+
});

Working-with-Events/projects/events-01-starting-setup/events.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<div>
1313
<h2>Events in JavaScript</h2>
1414
<button>Click me</button>
15+
<button>Click me Too!</button>
1516
</div>
1617
</body>
1718
</html>

0 commit comments

Comments
 (0)