Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When working with AJAX sometimes you want to access an individual element and sometimes you want to access groups of elements. We looked at how to do individual elements with getElementById in the last video, and in this movie we are going to take a look at accessing groups of elements with getElementByTagName. So you could, for example, ask for the third list element in an unordered list. We are going to need to update our HTML code to do something like that. So, I'll get rid of this div right here and then I'll add an unordered list with some elements. Now I am going to save this and switch over to my script code and modify this getElementById and make that getelements. Remember, there is an S in there.
ByTagName, the tag that we want to access is the list item tag. So by doing this, we are going to place an array of elements into the modify variable. Now that we've got the array, we can access an individual element of that array by just putting in the index of the element here. So, if we do modify(2) it will access the third element of the array, because these are 0 index, so the zero element is actually the first element, element number 1 is the second element, and element 2 is the third element.
So, if I save this and I switch over to my browser and I refresh the page, you will see that the third element in my list has been updated with the content of our data file. So we grabbed all the list items and we targeted just the third element. Interestingly enough, if you have more than one list on the page, it'll actually let you cycle through all the elements as if it were a single array. Let me show you how that works. Switch back over to my editor. So, in the index.HTML document I am going to get the original list and then make another copy right here.
So, let me save that and I'll go over to my script element. I know that there are six items in the unordered list. So if I ask for the eighth element, let's take a look at what happens in our browser. So I'll refresh, and now we have two lists and in the ninth position--that's 6, 7, 8, 9-- it's placed the text of our data file. There are some more objects down here. So there is the other bullets. But it updated the ninth element. Now there is actually a couple of other ways that you can get to the same place, so let's go ahead and examine that. So right here, when I am using modify=document.getElementsByTagName, I am looking for specifically all the list item elements.
I can actually do multiple copies of this, so I am going to do a document.getElementsByTagName. And first, I am going to look for the on order list elements on the page--right now there's two of them--and I am going to look for the first unordered list element and inside that element I'll get the ElementsByTagName('li'). That will target our second list, not the first one. And now I will switch the index here to be index number 2. So this is actually doing the same thing in a slightly different way.
First we are getting to the second list by using the index (1) here, and then we are getting to the elements inside that list that are of type list item and getting to the third element inside that object. So, if I save this--and I am going to go back into my page and just refresh-- you'll see that nothing is really changing, but the way that we are getting to those elements is different. So what if we wanted to cycle through all the elements in a list? Say that you wanted to update all the elements in our list items with the content from the external document. In that case you can use a loop.
So, let's go back into our code. So we went to, after the modify variable gets instantiated, when I create a for loop, and that loop is going to go from 0 to the modify variable's length--and remember that this modify variable is now an array, so any methods or properties that I would use on an array would be valid here. And what I wanted to hear is modify, using each index, the innerHTML of each one of those objects with the response text from the request.
And I also want to make sure that I modify my variable here, because it's only targeting the second list element, so I want to modify that to just look for getElementsByTagName, and I'll delete this line right here. So, what this is going to do is look for all the elements in your page that have a TagName of list item. It's going to place all that stuff into the modify variable, and then it's going to cycle through each modify variable, changing the HTML of each one of them to the responseText from our request. So let's save that, switch over to our browser, and we'll refresh, and now you'll see that it's actually updating every one of these bullet items.
So, getElementsByTagName is really handy for cycling through a series of DOM elements. Understanding how to handle the array it returns is one of the keys to modifying your DOM with AJAX.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98235 Viewers
61 Video lessons · 85544 Viewers
71 Video lessons · 69495 Viewers
56 Video lessons · 101838 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.