Syntax Video
Finding Elements in the DOM with JavaScript
Transcript
in this video we're going to be finding Dom elements using plain JavaScript we're going to be using both some new and old techniques some things that uh have been inspired by libraries of the past like jQuery but either way we're going to be able to find a Dom element and then modify it so let's get [Music] started okay so in this first video what we're going to be doing is diving into the do and we're going to be doing so in a way that shows you how you can interact with elements on the page using JavaScript and for the most part we're not going to be doing anything too intense but what we are going to be doing is selecting Dom elements and we're going to be showing you different modern ways of doing this so we're going to select this first one here that's Dom if you don't want to do that you can also just head to your local host Port
3000 SL examples slom SL index .html now this is what the page should look like for you now I'm going to slide this over here just so we have room for our code and I'm also actually I'm going to take that back I'm going to move this here and I'm going to put the browser window over here okay that way we can see things very clearly all right so now that we're here let's talk about what we're going to be doing for the most part we have some HTML over here and we're going to be wanting to interact with it for inance since we have one H1 or it's an H2 that says find me another one that says change me and then we have an unordered list that has no content we're going to be showing you how you can find this item how you can change this item and then how you can uh add multiple items down here after finding them all and we're going to be doing so using modern techniques now in the past you would pick up something like jQuery
to do this which is a library that makes finding and working with the D easy modern-day Java script though however has really made it simple to work with the Dom by taking actually some hints from jQuery and how it worked so the first thing we're going to do is find the find me element and we're just going to console log the output to the console if you want to pop open in your console you can click inspect down here and open up your console and I'll bump up the size of this so it's nice and easy or you can select console from right here as well and here we go so the first thing we're going to do is we're going to create a new variable so we're going to say const find me and I have an underscore in there and find uncore me is going to be equal to document do query selector and then pound find me now the Syntax for this is saying that we have the document and the document really is
referencing the HTML page itself so anytime you see the word document you know that you're going to be interacting with the rendered HTML that has been rendered into the browser in What's called the Dom or the document object model the document object model is just a fancy way to say that when the browser reads HTML it puts everything into an object that object has different properties on it and that is essentially going to be the structure of the entire rendered HTML page so anytime you see this you'll typically see a document Dot and then the property now the the method here that we're going to be using is called query selector now this isn't the only way to find things in the Dom we'll be doing more but query selector allows you to find things based on several different items several different selectors that might be a class um we'll be using that later on in this video it might just be an ID and since we have
given find me an ID of find me down here then we can access that with a pound find me okay so once again an ID of find me is located right here and then that means we're able to say pound find me query selector okay what that does is it assigns the item the document node of find me into this variable so if we were to do a console log here and this console log will then output this element so you can see we see our logs down here say find me is equal to to an H2 and it has an arrow if we click this Arrow this will open up and show you all of the properties that this Dom node includes we're going to be using some of these in this video and some of these throughout this course but there's a lot of stuff here this is what every single Dom node is most likely going to have in your application at any given point you
can access all sorts of things like the parent node or the child node or various elements of this you can even access its properties or its text content from here and this is how you largely work with a Dom node now we're going to be modifying Dom nodes in just a second here let's go ahead and find another way to find me now typically if you have something that has an ID such as the ID of find me here an ID in HTML must be unique that's one of the reasons why you would use an ID over a class for anything is that an ID is unique or a class like as you can see here with the change all can be reused so classes can be reused IDs must be unique and therefore we can look for things based on an ID and know that we're only going to find one of them and therefore we can also use a different method now we're going to assign this into a new variable by find by ID is going to be equal to and this time we're going to be using a
document. git element by ID so we're going to be getting the element by its ID and we're going to be passing in the ID here notice how in this one there's a pound find me and in this one it's just find by ID so we're not look actually this isn't even going to find it because the ID itself is also find me there we go sorry so document get element by ID find me so we don't need to give it pound because it already knows it's an ID where query selector doesn't know what we're looking for could be a class an ID an attribute or anything where a document get element by ID knows it's going to be an ID so we can simply just pass and find me now with that in mind we can console log this find by ID and it's going to give me the exact same element output here so there's no practical difference here between using query selector and get element by ID other than this requires you to have an ID okay well let's get into the change me part of this and you can use
whichever you would like the get element ID or query selector uh whichever you so want to choose and we're going to say change uncore me is equal to and we're when you say document document dog element by ID and then we're going to say change uncore me now that we have access to the Dom node of change me we can access any of its properties so when we looked in some of the properties before of our H2 you saw things like inner text and inner HTML now we're going to be talking a lot about the difference between inner text and in HTM ml in another video but for right now we're going to just say inner text so we're going to say dot inner text is equal to changed and you can see instantly what happens is when the JavaScript runs over here and now says changed if we were to
refresh the page you know what it's going to happen so fast I doubt you'll see any flash of it saying change me because this this is like very minor JavaScript code AKA it's going to run extremely fast but if we were to inspect the HTML of this it would still say change me in the HTML because that's what's initially being parsed or the initial Source that's being read by the browser okay so we now have find me done and we have change me done let's go ahead and add some HTML in here you'll see another div where we have an add HTML and likewise you can use get element by ID or query selector but we're going to do this one with get element by ID again in cuz I typically like use use that one HTML is equal to document. get element by ID and then the element that we're going to be grabbing is addore HTML now we're going to say addore HTML
do inner HTML and we can assign this to just some HTML and this is HTML as a string so we can say something like H3 this is added via JavaScript okay let's actually make sure it has to be valid it has to be valid HTML so we have to close the H3 and we can then save we should now see is an H3 that's been added to our div here that says this has been added via JavaScript once again if you look at the source that is not going to be included however if we inspect the element here it's going to be existing in the rendered Dom as you can see sorry it's a little cramped down here as you can see this is added via JavaScript right here okay so what happens is the document renders and then the JavaScript runs and then inner HTML is set to this
string the string is then parsed by the browser to find any HTML and then the document nodes are created for any HTML that exists in our HTML string okay so pretty cool so far right this is all feeling very very um I don't want to say basic cuz this might be your first time doing this but if you are an experienced developer chances are you've done some of this before and if you're coming from something like jQuery and never touching vanilla JavaScript in this sort of way you might not have known that you can easily query elements in the browser using just straight up vanilla JavaScript like this now you might also be wondering like what exists on any of these elements here right like how did I know HTML exists well we'll get to that in a second but the long story short is you have a couple of options you can console log the element as we did here and look through it or you can open up this mdn
docs throughout this course we'll be linking quite a bit to the MDM docs and this is going to show you all of the different Dom interfaces the Dom interface that we're actually working with here is an element or a Dom node if we select element you can see for example the HTML element interface is the base interface for HTML we can select that one and this will show you all of the properties that are on a given HTML element and you might expect to see some of the ones that we have in here but not everything is in here that's because of how properties exist within HTML you can see it inherits the properties of its parent element so anything that you don't see here is going to exist inside of element okay which is the root one now the element is where you will see things like outer HTML inner html text um or inner text I'm sorry not just straight up text
where is that inner HTML right here um ID accessing any of those types of properties so some of it is just becoming more familiar with the browser and some of it is really knowing where to look inside of the Dom and a little bit of experimenting keep in mind you you can always console log or you can always spy into things using JavaScript itself to see what something has available to it okay so lastly let's go ahead and find all of these list items and we're going to add text to each of these list items so we're going to be doing so using something called query selector all so we're going to say const find all is equal to document query selector all
query selector all works very much the same way as query selector but it finds more than one thing and it returns an array or a node list essentially an array of elements so what we can say here is we want to Target a class classes are referenced the same way that they are in CSS with a period You' say dot uh change hyphen all okay notice how I did a hyphen here that that might trip you up I used underscores for the classes or for the IDS and a hyphen for the classes just to keep them separate okay so find all and this now if we log this out this will return you can see a node list which is essentially an array of our nodes so if we want to add text to all of these nodes we can do a loop over them you can do a loop using various array methods or Loops in JavaScript to any degree in
which you know how for this video what we're going to be using is the for each array method so we're going to say findor all. for each which means we're going to Loop over each of the items in this array inside of that it's going to have an inline Arrow function here like this so basically what this code says is give me each item and iterate over it now to access each item we then have an arrow function that is called so we have the parentheses the arrow which is made up of an equals and a greater than sign and then our brackets and this will fire off an inline function now inside of these we end up having two arguments the item itself as well as the index the index is just the uh the in individual given Loop that we've done so to say if this is the first time we've iterated it'll be zero the second time it will be one so frequently when you hear index
it's the position in the array it's the index of the item in the array so if we were to say item. inner text is now equal to a string of added from for each and then we can have a hyphen here and then the space and then just Plus in the index like this what you should see after we save this is added from four each 0 1 2 3 4 okay so when we do a query selector all and get an array of each element we end up are able to work with them very much like a normal array and inside of those is the individual Dom node and we can interact with those individual Dom nodes the exact same way that we would as if we were referencing that Dom node independently and therefore we're able to set its inner text to a string and we can see that they're all dynamically set by just adding in this index now this index is doing nothing more than adding the 012 3
4 here okay it's not like this needs to be here but it's kind of fun just to be able to see that um these are all different in being modified independently from one another cool so this was a lot of stuff but definitely a good fundamental skill for a lot of things that we're going to be doing over the course of this video series now in the next video what we're going to be doing is diving into events before getting a little bit more into some other items within the Dum before starting to access some actually interesting apis so these are some core skills and these aren't the only ways that we can find a reference Dum nodes you may have seen uh while I was typing there were some autocomplete for things like get element by class um and those types of things right there there are also other ways to find elements in HTML but these are the ones that we're going to be using through the course of this series and we won't be venturing too far
out from any of these so this is how we work with the Dom and then we're going to be working with events as well as working with creating D elements from scratch in JavaScript in a way that's a little bit more I want to say uh more full featured than just passing a string into an inner HTML so as always this is Scott with level up tutorials thank you so much for watching and I will see you in the next one
Quote it with credit and a link. Quoting Syntax