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 trialRaymond Khoo
1,239 PointsWhat 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.
var fullName;
var lastName;
<!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
wuworkshop
3,429 PointsOkay, 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?
wuworkshop
3,429 PointsYou 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")
Raymond Khoo
1,239 PointsThanks for the reply.. the web kept on saying "Bummer! Try using the getElementsByTagName() method and using the second index (1)."
Kelly von Borstel
28,880 PointsI'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.
Raymond Khoo
1,239 Pointsso is this code correct? var lastName = document.getElementsByTagName("last_name")[1];
Kelly von Borstel
28,880 PointsNo, you want to use h1 (that's the tag) in place of last_name (which is a class).
Kelly von Borstel
28,880 PointsThe tag name is "h1"
Raymond Khoo
1,239 PointsStill showing me the same error even when I changed to "h1": Bummer! Try using the getElementsByTagName() method and using the second index (1).
Kelly von Borstel
28,880 PointsKelly von Borstel
28,880 PointsSorry, Raymond -- I took you the wrong direction. Good that imouto figured it out!
Raymond Khoo
1,239 PointsRaymond Khoo
1,239 PointsNo Problem Kelly.. you are still helping me so thank you BOTH of you.