Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

8.6 DOM Events

DOM events are extremely important. They are what allow us to create compelling, interactive web pages. We’ll see how to use DOM events to react to user and browser events in this lesson.

Key terms:

  • addEventListener
  • stopPropagation
  • preventDefault
  • removeEventListener

1.Introduction
2 lessons, 07:42

1.1
Introduction
02:12

1.2
Setup
05:30

2.Language Fundamentals
8 lessons, 1:00:53

2.1
Variables
06:33

2.2
Data Types
11:28

2.3
Arithmetic, Assignment, and Comparison Operators
10:24

2.4
Unary, Logical, Comma, and Spread Operators
09:02

2.5
Operator Precedence
03:50

2.6
Reserved Words
04:17

2.7
Strict Mode
04:34

2.8
Functions
10:45

3.Data Structures
5 lessons, 22:52

3.1
Arrays
04:29

3.2
Objects
04:30

3.3
Sets
04:57

3.4
Maps
04:21

3.5
Weak Maps and Weak Sets
04:35

4.Controlling Program Execution
7 lessons, 37:06

4.1
Conditionals
07:49

4.2
Switch Statements
04:41

4.3
The For Loop
06:39

4.4
The `for .. in` Loop
05:17

4.5
The `for .. of` Loop
04:02

4.6
Iterators
05:03

4.7
While Loops
03:35

5.Using JavaScript
13 lessons, 1:44:36

5.1
Working With Strings
09:32

5.2
Template Literals
05:46

5.3
Working With Numbers
06:57

5.4
Working With Arrays
12:53

5.5
Iterating and Transforming Arrays
07:33

5.6
Working With the Object Type
13:55

5.7
Object Literal Extensions
06:45

5.8
Working With Object Instances
06:45

5.9
Getters and Setters
05:00

5.10
Custom Objects
11:28

5.11
The `Math` API
04:54

5.12
Working With Dates and Times
08:10

5.13
The `Array` Constructor
04:58

6.Functions
8 lessons, 56:07

6.1
The `this` Object
06:15

6.2
Working With Functions
10:11

6.3
Scope
07:37

6.4
Arrow Functions
06:59

6.5
Generator Functions
08:13

6.6
Closures
05:00

6.7
Prototypes
06:26

6.8
Default and Rest Parameters
05:26

7.Miscellaneous
6 lessons, 52:39

7.1
Destructuring Assignments
08:09

7.2
AJAX
08:30

7.3
Regular Expressions
10:51

7.4
More About Regular Expressions
08:38

7.5
Classes
06:48

7.6
ES Modules
09:43

8.Working With the DOM
6 lessons, 37:39

8.1
Selecting HTML Elements
05:02

8.2
Manipulating HTML Elements
07:40

8.3
DOM Traversal
05:25

8.4
Adding and Removing Elements
04:45

8.5
Creating Elements and Other Nodes
04:39

8.6
DOM Events
10:08

9.Web APIs
4 lessons, 17:41

9.1
The Selector API
03:03

9.2
Geolocation
05:29

9.3
Web Storage
05:24

9.4
Web Workers
03:45

10.Asynchronous JavaScript
5 lessons, 26:23

10.1
Promises
09:52

10.2
Promise Chaining
05:11

10.3
The async Keyword
03:21

10.4
The await Keyword
04:04

10.5
More About async and await
03:55

11.Conclusion
1 lesson, 00:43

11.1
Conclusion
00:43


8.6 DOM Events

Hi folks! In this lesson, we're gonna see how we can make our pages nicely interactive by responding to events triggered by different elements on the page as users interact with them. Using DOM events is a cornerstone of modern webpage development. In order to make use of events generated in the UI of our web applications, we need to add event handlers for different user actions, like clicks on particular elements. Traditionally, this was a bit of a cross browser headache because IE exposed different methods for attaching events than other browsers. Thankfully these days, all modern browsers use the same API, so it's much easier than it used to be. So let's add an event handler to one of the elements on our test page. So we've now got a reference to the heading on the page, let's add an event listener for click events. So we've added an event list now for click events using the addEventListener method. This method takes two arguments, it can take more, but it’s very commonly used with two arguments. The first argument is the name of the event that we want to listen for, in this case, it's click events. And the second argument is a handler function, in this case, it's an array function. Event handler functions are automatically passed an event object. And we can also log this out to the console as well, so that we can take a look at it. So we should find now that when we click on the heading, we see a message in the console. So the event object was part of our log statement. And let's open that up, and we can see that it contains many, many different properties. And this is all useful information about either the event itself or the element that the event occurred on. For example, we can see the target property down here, and that points to the element that triggered the event, in this case, the heading. The addEventListener method can also accept a third argument, and that specifies whether the event should be handled in the bubbling or the capture phase. Generally, we don't need to worry about this, but it is useful to understand what happens when an event is triggered. So when the H1 element was actually clicked just then, the event traveled from the document down to the target element, which in this case, was the H1. So this is known as the capturing phase. As soon as the event reaches the element that triggered the event, it enters the at-target phase. The event then travels back up from the element that triggered the event all the way back up to the document. And that's the bubbling phase. So as I mentioned, generally we don't need to worry about these different phases. Although, we can make use of the fact that these phases occur in a technique known as event delegation. And that is where we attach event listeners to parent elements instead of individual elements. And we wait for the events to bubble up from the children elements, up to the parent, which contains the handler. So one example is that we might want to add listeners to the list items in a list. So we do have a list on the page. So instead of getting the heading, let's get the list instead. So we've added a click handler to the unordered list. Let's take a look at what happens when we click on any of the list items. We just need to get the first list item out of the collection returns by getElementsByTagName. So we've added the click handler to the list. Not the individual children, but we can still click on the individual child elements. And that will still trigger the event list now on the parents. And if we open up the event object, we can see that the target points to the list item that was clicked. So this can be a very useful technique. So when an element is clicked and triggers an event, even if a parent handles that event, the event will still continue to bubble all the way up to the document. So let's add another click handler to the document. So let's see what happens now. We see both of the log messages. So we see the log message added in the handler for clicks on the list item for clicks on the list. But even once the list has handled that, the click event still travels all the way up to the document element and triggers the handler that we added there. And we don't always want that to happen. Luckily, there is a very easy way to stop the propagation of that event. So we've used the stopPropagation method of the event object. And that will stop the event from travelling any further than this handler. So now, if we click somewhere on the document, we'll see the generic click happened log message. But if we click on one of the child elements, we only see one event now, because the event gets stopped by the handler attached to the list. And it doesn't bubble up any further from that point. The event object has another useful method called preventDefault. This is useful for when the element is an anchor. Usually when an anchor is clicked, the browser's default action is to follow the href of that anchor. And to either navigate to a different part of the same document or a different document altogether. We can stop that default behavior using the preventDefault method. So let's just create a quick anchor and append it to the page. So we've created a new anchor element, we've given it some text, and set its href attribute and we've appended it to the page. And we can see the link down at the bottom of the page here. And if we click on that, it takes us to Google as we would expect. So let's now stop that from happening. So let's go back to the page now. And this time when we click it, the document doesn't take us to Google.com. If we want to remove an event listener, we can use the removeEventListener method. However, for this to be effective, we need to know which handler function we are removing. And so we should use named function references instead of the arrow functions that we've been using so far. So it should still work in exactly the same way as it did before. But now, if we want to remove that handler, we've got a function reference that we can use. And now we shouldn't see any more messages. I accidentally clicked the list then, but when I click the document, the handler has been removed. So in this lesson, we looked at adding listeners, or handlers, for events triggered on the page by the actions of our users. We focused on mouse events, because they're quick and easy to work with. But we can also make use of many other types of events, including keyboard events, and scroll events. We saw how to add event listeners using the addEventListener method. And we saw that we should provide a name for the event that we want to listen to as the first argument. And the function to invoke whenever the event occurs as the second argument. We saw that event handling functions are automatically passed an event object. Which contains useful properties that give us information about the event. And methods that we could use to either stop the event from propagating up the document or to prevent the default behavior of the browser. Lastly, we saw how to remove event listeners with the removeEventListener method. But then we need to specify exactly which listener we want to use, and so this only works with function references and not with anonymous arrow functions. This now brings us to the end of the DOM section. The next section is the final section of the course, and it focuses on using some common web APIs. Thanks for watching.

Back to the top