Skip to content

Commit b3615ee

Browse files
committed
Event Handler Functions and this
1 parent d5e27d3 commit b3615ee

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Working-with-Events/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
| [Event Propagation and `stopPropagation()`](#event-propagation-and-stoppropagation) |
1313
| [Using Event Delegation](#using-event-delegation) |
1414
| [Triggering DOM Elements Programmatically](#triggering-dom-elements-programmatically) |
15+
| [Event Handler Functions and `this`](#event-handler-functions-and-this) |
1516

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

@@ -666,3 +667,38 @@ Let's say from an event, there is a need to trigger another event. For example,
666667
```
667668
668669
It has two buttons - "Click to know me!" and "I click other buttons" - and a script that adds an event listener to each button. When the "Click to know me!" button is clicked, it logs a message to the console. When the "I click other buttons" button is clicked, it logs a different message to the console and then programmatically triggers a click event on the "Click to know me!" button using the [`click()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click) method. Overall, the code works as expected.
670+
671+
## Event Handler Functions and `this`
672+
673+
When an event is triggered and an event handler function is called, the `this` keyword inside the function refers to the DOM element that the event was triggered on. This is useful when we want to access or manipulate properties of the element inside the event handler.
674+
675+
For example, suppose we have a button element with an event listener that changes its background color to blue when clicked:
676+
677+
```HTML
678+
<button id="myButton">Click me!</button>
679+
680+
<script>
681+
const myButton = document.getElementById('myButton');
682+
myButton.addEventListener('click', function() {
683+
this.style.backgroundColor = 'blue';
684+
});
685+
</script>
686+
```
687+
688+
In this example, the `this` keyword inside the event handler function refers to the button element, so we can access and manipulate its `style` property to change its background color.
689+
690+
In case of nested DOM elements, the value of `this` inside an event handler function will depend on how the function is called. If the event handler function is bound to the innermost element, `this` will refer to that element. If it is bound to an ancestor element that contains the nested elements, `this` will refer to the ancestor element.
691+
692+
For example, consider the following HTML structure:
693+
694+
```HTML
695+
<div class="outer">
696+
<div class="inner"></div>
697+
</div>
698+
```
699+
700+
If an event handler is bound to the `div.outer` element, and the user clicks on the `div.inner` element, the value of `this` inside the event handler will still refer to the `div.outer` element. If the event handler is bound to the `div.inner` element, `this` will refer to the `div.inner` element.
701+
702+
It is important to keep in mind the event bubbling and capturing phase as well when considering the value of `this` in nested elements.
703+
704+
It's important to note that when using arrow functions as event handler functions, the `this` keyword behaves differently. Arrow functions inherit the `this` value of the enclosing lexical scope, which is often the global object or the object that the arrow function is defined in. Therefore, if we use an arrow function as an event handler, the `this` keyword will not refer to the DOM element that the event was triggered on.

Working-with-Events/projects/events-01-starting-setup/assets/scripts/events.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ const list = document.querySelector('ul');
103103

104104
// Event delegation will work unexpectedly when in our example, list items are nested
105105

106-
list.addEventListener('click', event => {
106+
list.addEventListener('click', function(event) {
107107
event.target.closest('li').classList.toggle('highlight');
108108
// Triggering button programmatically
109109
button.click();
110+
console.log(this);
110111
});

0 commit comments

Comments
 (0)