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

Challenge doesn't seem to work: interactive web pages with javascript

Hello,

In the following challenge: https://teamtreehouse.com/library/selecting-elements

in task 2: we are asked to: "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."

When I input my answer I get the following error message: "Oops! It looks like Task 1 is no longer passing".

I think my code is correct. I put:

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

Is there a problem with my code or with the challenge?

Many thanks

3 Answers

Hey Dekin,

you almost got it right but the method to get an element by the tag name is actually called "getElementsByTagName", so you just missed the "s" after Element.

This should work:

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

Wow thanks. Now that's confusing! One is "getElement" and the other "getElements"... with no logical reason why...

Well spoted. Thanks

No problem! Yes, it's a bit tricky. But actually there is a logical reason which is that the method getElementById returns only one element in the code because an ID is unique whereas the getElementsByTagName returns an array of all the elements that match the tag name you specified.

Aaaah, thanks! wish it were as well explained by the coach in the videos...