Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Modifying elements with getElementsByTagName

From: JavaScript and AJAX

Video: 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.

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.

Show transcript

This video is part of

Image for JavaScript and AJAX
JavaScript and AJAX

21 video lessons · 19082 viewers

Ray Villalobos
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript and AJAX.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.