- Overview
- Transcript
6.5 Listening for Events
Events are probably the most important part of JavaScript development because they allow us to react to the user. In this lesson, you'll learn how to listen for and react to events. There are a ton of events, so we'll just stick with the most common: the click
event.
Related Links
1.Introduction2 lessons, 06:25
1.1Introduction01:51
1.2What You Need04:34
2.How the Web Works2 lessons, 16:03
2.1Networks and DNS09:20
2.2HTTP and the Web Server06:43
3.Creating Documents3 lessons, 27:13
3.1Marking Up a Document09:38
3.2Making Our Document Valid06:22
3.3Semantic and Generic Elements11:13
4.Styling Documents3 lessons, 34:07
4.1Adding Style08:17
4.2CSS Selectors16:15
4.3Modifying Layout09:35
5.Scripting Documents3 lessons, 22:36
5.1Variables and Functions09:06
5.2Writing Your Own Functions05:34
5.3Objects07:56
6.The Document Object Model6 lessons, 42:45
6.1DOM Basics08:58
6.2Finding Elements07:40
6.3Working With `NodeLists`04:30
6.4Manipulating an Element's CSS Classes04:29
6.5Listening for Events08:42
6.6Practicing the Concepts08:26
7.HTTP Requests With JavaScript1 lesson, 09:16
7.1The fetch() API09:16
8.Introduction to Server-Side Development5 lessons, 43:24
8.1Introduction to Server-Side Development10:31
8.2Your First Line of PHP Code06:57
8.3Functions and Variables08:00
8.4Parameters and Decisions08:13
8.5Getting Data From the User09:43
9.Getting Started With Databases3 lessons, 31:01
9.1Create a Users Table12:00
9.2Create a Posts Table10:20
9.3Inserting Data08:41
10.Using PHP to Interact With MySQL4 lessons, 51:59
10.1Reading and Displaying Data12:34
10.2Passing and Validating Data11:58
10.3Updating Data14:52
10.4Joining Data (And Deleting Posts)12:35
11.Conclusion1 lesson, 01:47
11.1Conclusion01:47
6.5 Listening for Events
Events are probably the most important part of JavaScript development, because they allow us to react to the user. And even though we don't really see it, most everything that the user does causes an event. So if they click on anything in the page that causes an event, if they type, if they scroll, if they're on a mobile device and they tap, all of those things are events. And the great thing is we can write our own code and hook into that functionality, so that whenever the user clicks, or types or does practically anything, we can execute our own code. So in this lesson, we are going to set up a click event for our message elements, so that whenever we click on it, we are going to toggle the ugly text class. So that's going to be practically useless but this is going to introduce you to events which is half of the battle. So the first thing we need is the object that we want to set up the event on. And we have that, that is our messageElement. The next thing we have is a method called addEventListener. It's a mouthful to say, it's also a finger full to type. But we are setting up what's called an event listener that's going to listen for a particular event and we get to specify what event that is. So if the user clicks on our message element, well, that's what we want to listen for. And then we want to execute some code. Now, we learned a couple of lessons ago about a callback function, it's a function that we pass to another function so that it can call it, that's kind of weird to say. And we essentially need to do the same thing here, we need to write a function that will toggle the ugly text or we might want to do it like this messageClick, now, something to indicate that this is handling the click event on our messageElement. And all we really want to do here is toggle that ugly class. So we'll use our class list, we'll call the toggle method and then pass in ugly-text, so there we go. But that's not all, we need to take this messageClick function and we need to pass it to the event listener. Now once again, notice that we are just using the name of the function, we are not executing that function because, well, that's wrong, that's going to result in our code not working. So let's revise, on our messageElement, we are adding an event listener for the click event and we are going to execute our messageClick function whenever that event occurs. So let's click on the div, and we can see that we are indeed toggling the ugly text class. It's not changing the bold, in fact, if we go ahead and inspect this, notice down here for this div element. Watch that as I click, well, let's do something a little bit more involved. Instead of setting up the clickEventListener on our messageElement, let's do so on the individual span elements. So the first thing we need to do is get all of those span elements. So we will use querySelectorAll, our CSS query is the ID of message and then span. And then that will give us all of those span elements. And the next thing we want to do is set up the clickEventListener on every one of those span elements. And the only way to do that is to use the forEach, well, it's not the only way, but it's the way that we know because for each span element, we need to set up that event listener. Now, I'm going to do something a little bit different here. I'm going to do what's called inlining of function. The reason why it's called that is because it is in line with the rest of the code. And the technical term for this function is an anonymous function because it doesn't have a name, but it is still a function and we are still passing it to for each. The reason why I'm using this style here is because you will see this in probably 95% of all of the code on the Internet. This is just the style that everybody uses. It is technically not the same thing as defining the separate function calling it process span or better yet set up click but it is functionally the same. So we are creating an anonymous function for the forEach method, and we need the element that we're going to be working with. And then we want to set up the click event listener, so we're going to call addEventListener. We want to listen for the click event and then we want to specify a function. Now, I am not going to use an anonymous function here because I want you to learn about something else. So we are going to write a function called clickSpan. And we need to be able to toggle the ugly-text class on the span element. Now, one very important thing here, is that we don't have access to that span element, at least inside of this clickSpan function. We do down here when we are inside of the forEach callback, and we could get away with using that if we used an anonymous function down here, I don't want to do that. The reason is because whenever you set up an event listener and that event fires and it calls our callback function, it passes a special event object to that callback function. We typically just refer to that as e, and this event object has all of the information that occurred about the event. So for every event there is a type property that will tell us the type of event that occurred. Now, that might not seem very useful because we know that this is a click event. But I guarantee you there are times when you don't know the type of event that occurred and you need to. Now, if this event was caused by somebody typing on the keyboard, it would have the information about the keys that were typed. If the event occurred because the mouse was moving over certain elements or across the screen, it would contain the information about the mouse coordinates. But in our particular case, what we want is the element that received the click event, it is the target of the event. And we can get that using the target property. Every event has a target, so no matter what type of event occurs, you can always get the element that received that event. So now that we have the target of the event, we can use the classList and we can toggle the ugly-text class. So with that in place, let's comment out everything else, because we want to focus just on what we have for our span elements. So this means that our starting text is just normal text, there's no styling applied at all. But whenever we click on the individual span elements, we can see that the ugly-text class is applied to it. So there are essentially three things that you need to remember from this lesson. The first is that almost everything the user does results in some kind of event, be it clicking, be it moving the mouse around, be typing, tapping, scrolling, it doesn't matter. Whatever the user does, there is an event that occurs. The second thing is that you can listen for those events. And you can handle those events by executing your own code. You have to set up an event listener to do that. Specify the event that you want to listen for, and the code or the function that you want to execute when that occurs. The third thing that you need to remember is that every event listener receives the event object and that event object has all of the information about the event that occurred. There are a ton of events that can occur and I will add a list of them in the description for this lesson.