Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![s speir](https://secure.gravatar.com/avatar/f5be031ef6d685fe3fd516d10d529649?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
s speir
3,991 PointsWhy can't we just use getElementsByClassName, instead of getElementsByTagName?
I went on to the challenge just after this video, and don't understand why I can't use get ElementsByClassName to select and assign the second <span> with the class .last_name.
Why bother dealing with getElementsByTagName which requires you to deal with index numbers and such? Or is it simply because getElementsByTagName is used in the video, and you're supposed to copy it 'because'?
Thank you!
2 Answers
![andren](https://secure.gravatar.com/avatar/229e60726cd92951c31d7303e40be2cd?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
andren
28,558 PointsYou can in fact use getElementsByClassName to solve this challenge.
But one thing you seem to have misunderstood is that both the getElementsByTagName and getElementsByClassName methods requires you to use indexes to access elements.
Both of those methods returns a list of elements, even if there is only one element matching the tag / class you specified. The getElementById and querySelector methods are the only DOM selection methods that only return a single element, and therefore does not need to have an index used.
Here is a solution to this task that uses the getElementsByClassName method:
var fullName = document.getElementById("full_name");
var lastName = document.getElementsByClassName("last_name")[0];
![Jesus Mendoza](https://uploads.teamtreehouse.com/production/profile-photos/2498332/micro_IMG_0205-copy2-240x240.jpg)
Jesus Mendoza
23,289 PointsHey speir,
I guess it's because the teacher wants you to practice and get familiar with the getElementsByTagName method.
With classes you also need to add the index of the element you want to select though!