Bummer! You must be logged in to access this page.

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

Charles Harpke
Charles Harpke
33,986 Points

Select the second SPAN element on the page and assign it to the variable lastName on line 2. Take a look in the index.ht

This is the message I receive: Bummer! Try using the getElementsByTagName() method and using the second index (1).

Here is the 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>

here is app.js:

var fullName = full_name;
var lastName = document.getElementsByTagName("last_name");

5 Answers

Kevin Kenger
Kevin Kenger
32,834 Points

Hey Charles,

You're using the right method, but last_name is the name of the class, not the tag. Try using span, and then select the 2nd one using its index.

var lastName = document.getElementsByTagName('span')[1];
Charles Harpke
Charles Harpke
33,986 Points

Excellent...thank you...I got so caught up in the method I wasn't thinking about the class...

Is always elements, when you are put the document.getElementsByTogName?

Kevin Kenger
Kevin Kenger
32,834 Points

Yeah, document.getElementsByTagName will only target the elements you specify.

thumb up!

Michael Hicks
Michael Hicks
24,208 Points

I guess this exercise is looking for a specific technique, but would the following code work?

var lastName = document.getElementByClassName("last_name");
Kevin Kenger
Kevin Kenger
32,834 Points

Hey Michael,

The code challenge is looking for that specific code, yeah. But you're right. You could use the getElementsByClassName() method to target that element.

Keep in mind, the method is getElement(s)ByClassName. In your code you wrote Element. I'm not sure if that was just a typo or not, but I saw it as a good opportunity to bring up the fact that it targets ALL of the elements with the specified class name. So in this instance, if there were other elements that had the class last_name, they would be targeted as well.

Michael Hicks
Michael Hicks
24,208 Points

I see. Your explanation was very helpful and its good that you pointed out my typo because I have made that mistake more than once. I completed the exercise properly, but I wanted to know if there were other ways to accomplish it and I appreciate your timely response. Thank you Kevin.