Syntax Video
Listening for DOM Events with JavaScript
Transcript
in this video we're going to be listening for events in JavaScript that is clicks change input those types of things and then we're going to be able to run functions based on those events so let's get [Music] going okay so in this video what we're going to be doing is diving into events within the browser now events are how you interact with the Dom and while this is going to be possible to do a fullon deep dive into events we're going to be talking more or less about how you can have events that are registered so that you can interact with the Dom and at least get started in what you'll need for the rest of this series so we have a folder called events here that has an index DJs that's empty as well as an index.html file in fact let's open up both of
these and close the sidebar here and then if you're following along in this course you can go ahead and select events 101 and you'll see here what we have we have a click me button that does nothing we have a number input that well it increments in numbers when you hit up or down or you can change it and we have a text input without a label not a very accessible text input but we're not about accessibility in this course right now we're just talking straight up uh events okay so what do we want to accomplish here well we want to accomplish some events within the click me we want to be able to listen to events on the input boxes and we want to be able to uh really work with JavaScript in a way that is framework lless as in you're not using a a react a view as felt any of that stuff but is still interacting and updating the Dom so let's go ahead and get started with
our clickme Button as many of these videos are going to do we're going to be starting with a const click uncore me and this is going to be from our last video document get element by ID click me now just about throughout the course of this entire series we'll be using IDs attached to things so if you ever see me do a get element by ID you can assume that unless we're showing it on camera that click me is going to be existing on the element that we're talking about so what we want to be doing here is when you click on this click me button that uh we're just going to Simply councel log out hey this thing has been clicked okay so let's go ahead and add an event listener so an event listener is what it sounds like it's something that listens for an event and then outputs a function or runs a function so if we wanted to have a function being run we could determine a function here that would be
uh clicked okay this function is just called clicked and all it's going to do is console log the word clicked now if we want to add an event listener to this click button you can say click uncore me which is referring to the HTML element that we've gotten right here click me.
addevent listener now ad event listener takes two parameters that we care about right now one of which is going to be the event the event is going to be a string and this string is going to be click at least that's what we're using so when you click something and the second parameter is going to be the function that is run so in this case the clicked function so now anytime you click this button you should see clicked and you can see it only outputs once but then you'll see the number increases over here for just how many times this has been clicked we click this 20 times it will say clicked 21 or click 20 uh click 21 cuz I clicked it 21 times right so as you can see here we have told this here's a function to run here's a thing to do we listen for the click and then we run this function now throughout the course of this series and through basically the rest of this to avoid creating a bunch of functions like this
and then referencing them what we're going to be do is working with a lot of inline Arrow functions to do this same thing so if we were to duplicate this function here or the ad event listener click uncore me add event listener and then instead of passing a function into here we were to put an arrow function in here just like this we can now console log click directly from this and we can conso log this out like so and this is essentially for all intents and purposes in this course going to be the same thing as in when you click this each time you can see it outputs clicked the exact same way so while this is referencing a function that we're creating this is creating the function inside of the event listener the exact same way is just slightly different okay so this is a good start here let's go ahead and now what we want to do is now we want to work with this input perhaps every time we click this button maybe we
want to increment the value of this input so the first thing we're going to have to find is the const click uncore value which you can see is listed right here click value and we're going to say document dot get element by ID click value now this is a input type of number with a default value of zero you might think that this would lead to the value when we read this element of being zero but let's actually go ahead and do a quick experiment before we Implement something about the event listener here the quick experiment that we're going to be doing is to Simply check the type of The Click value value so if we console.log a type of Click uncore value. value you might
think this is going to be a number considering this is a number input but if you'll notice here what's output is actually string because anytime we're working with a number input of course uh it's going to come in as a string so which means that if we want to increment this thing what we'll want to do is we'll want to always convert it to a number now there's a lot of ways to convert things to numbers but an easy one is to use a function baked into JavaScript called parse int which will turn this thing into an INT now an INT is a non-decimal number so this isn't going to be uh this isn't going to work if you want to do something like 0.1 but this will work with 1 2 3 4 right a non decimal number so what we can say is let value equal to and we're initially going to instead of just setting this to zero what we're going to do is grab the initial value directly from the input itself that way if we update in the HTML that's kind of the single starting point
of Truth meaning that if we say parse int click value. value we should now see the value if we log this out and keep in mind we're going to be doing a lot of conso console logging in this course you can watch me do it or you can do it yourself if you want to see the values but again at the end of the day what we're doing here is largely just experimenting before we get into building actual things okay so what we have here is we now have a zero being output as not a string but a number you can tell that it's not a string cuz well it's like a green color it's a different color right so cool we have our value Now set to be a value next thing we'll want to do is we'll want to update that value so we can say value is plus equal 1 and now that value is going to be equal to a number greater than uh what it currently previously was each time we click it and it's going to update this each time we click it you can see this
being output directly if we just console log this value directly afterward saying click me and what you should see value one value two value three value four value five each time we click it it's taking this one value that we set and it's incrementing it by one now let's say we want to then pass in that value back into our input well this is actually pretty simple all we need to do is say click value. value cuz we're accessing the HTML element and then its property of value and setting it equal to the new value okay we can remove this console log and we can remove this other one that says clicked CU what you should see now is you should see that whenever we click this the input is going to increment by one just like that so this is fantastic we're now able to take a value from common HTML input update it wait for it and and update it on the click of something else and then reput
it back into the input cool right very cool okay so let's keep moving here and what we're going to be doing now is adding an event listener to the change me because we're going to be listening to a different type of event and we're going to be talking about events on inputs again this video is largely set up to give you some core skills in dealing with inputs so what we'll want to do next is grab a new Dom element const change me which is again right here and this is going to be document get element by ID change me now change me is just a very simple text input now we have access to change me in a very simple uh Dom element called change uncore me so change me we can add all kinds of event listeners now you can add as many event listeners as you want for different types of events you can say add event listener and you can listen for a change event like this and a
change event you might think fires whenever the value is changed however if we were to run this and we're going to be doing this again largely through console logs you might run this and you might expect if I type in here for change to be fired each time I type but it isn't actually in fact it only toggles if I've changed the value and I'm selecting off of it so the moment I blur off of the put you can see change me fired if I focus and then blur off of it it doesn't refire but if I focus and remove one and then Focus off it fires cool right so next one we're going to take a look at is going to be the blur event now blur is going to be an event that fires Whenever You tab off of the element sorry I wrote blue but it's blur that would be fun right okay so here you can see that um let me say blur and let me change this one that says change me
to change okay just so we can keep track of them so you focus and then blur you can see blur run but change didn't run now we change this we blur and you can see change runs first and then blur runs after all right so that's the blur event and now we have more events even further we have things like input now input is going to be the event that fires when ever you make a modification to the input itself as in if we type in here every single time we type it's giving us the latest value f f f f f f f f right so this is a really nice way to listen for each new character that's being typed into an input so to add some comments here this fires a change event fires on blur before blur only if value
changed blur fires fires on Focus off okay and then input fires fires on individual character change now there's another kind of interesting input that fires on individual character change as well that could be used to make modifications to the values but it doesn't get support in Internet Explorer so if those are you out there who still have to use Internet Explorer this one's off the table for you but believe it or not there's also a before input event now before input before input functions slightly differently than input where if we were to type in here you're going to see that before input fires immediately but then input fires but the difference is is that the change me value when before input fires gives us an empty string as in it fires with the value in the value as what it
was before the change happens where the input gives us the F which is the character that's been inputed so if you're looking and you ever see input and before input that's really the big difference the only difference in usage of these things is that before input itself does not work again in Internet Explorer which honestly I'm over supporting at this point okay so now there's a couple more things you need to know about events before we move on one of which is that the event function that fires gives us an event parameter past in to this function as then if we say event as a parameter here we have access to the event itself so now when I type and keep in mind I'm on the input and not the before input just to keep things not confusing here you'll see that whenever I type it's not just giving us information about the Dom element it's giving us a ton of information about the
event itself this event has all sorts of properties things like bubbles which we'll talk about in just a second data which is the data being passed in right if I were to say s the data is now going to be S but the value is much longer than S so the data you can see here is s but the value is this whole string we also have something called default prevented now prevent default prevent prents a function from propagating further which is a little out of the course of this series although we may be using it once or twice and I'll explain why when we do however if we continue to scroll down you can see there's a lot of things in this event one of which is the target itself now the target gives you access to the Dom element and you may have use this in your frontend framework work if you've ever done so because you can utilize things like like event.target
doval to get the same value that we have as change meore Val because event.target allows us to access again the entire Dom node properties from the Dom node that we've had the event on we're going to be using this in a kind of interesting way as we go now okay one more thing is going to be about bubbling I'm going to add one more event here and it's going to be so on the card that wraps around everything we have this class of card so we're going to First grab that using const and we're going to say card and this time we're going to say document do query selector and we're going to say Dot card to get the card there's only one of them so we're not using query select or all just query selector card and that gives us our card which we'll say card.
add event listener click and then we're going to run an inline function from this now you might be also wondering about all of the different wonderful events that take place in the browser well there's a lot of stuff and you're going to need to really dive into the documentation if you're looking for a specific type of event usually it it kind of helps to know what event you're listening for I'm listening for something with the keyboard something with the mouse uh I'm listening for a focus a blur those types of things right but it's again something that will come with experience I've also linked to the HTML input events spec here which will show you just a brief example of all of the different input events right and that's just for input elements in themselves okay so we have a card at event listener with a click on it and we'll once again want to take advantage of having the event inside of here and what's neat about this is that
we'll want to take a look at something specifically I'm going to say console.log and we're going to say event.target to see when I have a card here and we click anywhere in this white space it gives us the div of the card but if I have this button here and the button is a child of the card what happens do you think if I click this button if I click this you can see the button itself becomes output now you might be thinking well that's just what we had up top here but you'll know we removed the console log from here so this button is no longer console logging whenever we click it meaning that this console log as you can see from even the note here right here it says it came from line 44 in our index.js meaning this console log right here hey it says hey I thought I had an event listener to
the card not the button right but then when you click the button but it says hey uh I clicked so this is actually something called event bubbling where when you click the card the event bubbles up right so the click does not just take place on the card here it's taking place on the button and moving upwards and if we want to see that entire path wordss upwards we can actually do so using event. composed path which is kind of a newer function I don't believe this has full browser support but if we click this you can see the bubbling path here and you can see if we open this up the button was then clicked now the button is a child of the card which is a child of the body which is a child of the HTML and which is a child of the HTML document which is a child of window kind of neat right and this gives you a little bit of an insight into how the browser works okay so here we go we have a whole bunch of different event
listeners a bunch of different event types and this is a small percentage of the events that exist with in the browser this by no means is meant to be an entire comprehensive on the events of the browser but what we've shown here is that you can interact with dom elements directly by attaching an event listener to an individual element and then utilize things like just straight up JavaScript where we're incrementing a value and then setting that value back into something else okay again just a core skill that we're going to want to have before we get moving on some of the cool apis that exist in the browser so the next one that we're going to be doing is talking about the creating Dom elements in the browser creating Dom elements in the browser is not something that a lot of people do when they are getting started with JavaScript but once you do you realize that you can interact and create things and move around the Dum with your own creations of elements in all sorts of ways Based on data
that's coming in and you can do a whole lot with straightup vanilla JavaScript in this regard so as always this is Scott with level up tutorials in the next video like I said we're going to be talking all about creating and appending HTML elements via JavaScript
Quote it with credit and a link. Quoting Syntax