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

JavaScript Interactive Web Pages with JavaScript Selecting Elements and Adding Events with JavaScript Selecting Elements

What is wrong with my code in Challenge Task2? var lastName = document.getElementsByTagName("last_name")[1];

What is wrong with my code in Challenge Task2? var fullName = document.getElementById("full_name"); var lastName = document.getElementsByTagName("last_name")[1];

Problem is after I wrote the second code line, the web said "first code line is not working" even though I knew it was right. Please help me out.

app.js
var fullName;
var lastName;
index.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1 id="full_name"><span class="first_name">Andrew</span> <span class="last_name">Chalkley</span></h1>

<script src="app.js"></script>
</body>
</html>

4 Answers

Okay, I just did the challenge to see what they actually ask you to do. The correct answer for Challenge 2 is the following:

var fullName = document.getElementById("full_name");
var lastName = document.getElementsByTagName("span")[1];

Since the challenge asks you get to the last_name class using the span tag, you get all the span elements by using

document.getElementsByTagName("span")

The span element with the class last_name is in the second span element, so you add [1] at the end to target the second item. Does that make sense?

Kelly von Borstel
Kelly von Borstel
28,880 Points

Sorry, Raymond -- I took you the wrong direction. Good that imouto figured it out!

No Problem Kelly.. you are still helping me so thank you BOTH of you.

You don't have a tag name called "last_name". You have a CLASS name called "last_name". So you would use:

document.getElementsByClassName("last_name")

Thanks for the reply.. the web kept on saying "Bummer! Try using the getElementsByTagName() method and using the second index (1)."

Kelly von Borstel
Kelly von Borstel
28,880 Points

I'm not sure, but I think they want you to use getElementByTagName for the h1, since the second index [1] of that would be the second span, which has the last name. First index [0] has the first name.

so is this code correct? var lastName = document.getElementsByTagName("last_name")[1];

Kelly von Borstel
Kelly von Borstel
28,880 Points

No, you want to use h1 (that's the tag) in place of last_name (which is a class).

Still showing me the same error even when I changed to "h1": Bummer! Try using the getElementsByTagName() method and using the second index (1).