Modifying elements with getElementsByTagName
Video: Modifying elements with getElementsByTagNameWhen 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.
- Next steps
Viewers: in countries Watching now:
- What is AJAX?
- Making requests asynchronous
- Updating the DOM with getElementById
- Reading JSON files
- Working with jQuery and AJAX
- Sending and searching JSON data
- Incorporating CSS transitions
Modifying elements with getElementsByTagName
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.