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

Why does't this work?

Interactive Web Pages w/ JavaScript - Selecting Elements

I'm stuck on the first code challenge. The question is...

"Select the second SPAN element on the page and assign it to the variable lastName on line 2. Take a look in the index.html to see the structure of the document."

var fullName = document.getElementById("full_name");

this doesn't pass!

5 Answers

Erik McClintock
Erik McClintock
45,783 Points


There are a couple of things to look at here:

1) For the second task in the challenge, it is asking you to assign the second span element to the lastName variable; you are currently showing code regarding the first task - do you need help with the first or second task, or both?

2) If you are indeed having difficulties with submitting this code for the second task, the error should be prompting you with some useful information that pertains to the video you would've just watched. Namely, it reminds you to use the 'getElementsByTagName' method to select the correct element.

For the first task, your code is correct. To select the h1 with the ID of #full_name and assign it to the fullName variable, you are good to go:

var fullName = document.getElementById("full_name");

For the second task, remember: You want to use the 'getElementsByTagName' method, and then you want to append the index for the second element of that type, keeping in mind that indexes start at 0, so the second element will have an index of 1. And make sure you have it pluralized - don't forget the 's' in 'Elements'! Easy/common/frustrating mistake.

Try to put this together for yourself, and let us know if you're still having difficulty! If you can't get it put together, we'll help you along further :)

Hope this helps!


Here is the answer var lastName = document.getElementsByTagName("span")[1];

Jason Taylor
Jason Taylor
3,059 Points

Can you do

var lastname = document.querySelector('span:nth-child(2n)');

It actually worked after I refreshed the page and typed in the exact same line. thanks all!

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