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

Annette Burke
PLUS
Annette Burke
Courses Plus Student 312 Points

Error on Interactive Webpages with Javascript challenge

I get an error on the second line. What an I doing wrong?

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

3 Answers

last_name is not a valid tag. When using getElementsByTagName("..."); you need to select a valid HTML tag.

For example, <div>, <p>, <a> and <img> are all HTML tags. So you would select them by writing something like: document.getElementsByTagName("p"); to select all <p> tags in the document.

In this specific challenge that you're on I think you need to write is this:

document.getElementsByTagName("span")[1];
Annette Burke
PLUS
Annette Burke
Courses Plus Student 312 Points

The challenge is specifically telling me to use getElementsByTagName. I assume the second index is the second span. There's not much else in the HTML file. If I try using document.getElementsById("last_name"); I get the same error message.

Here's the error message: Bummer! Try using the getElementsByTagName() method and using the second index (1).

Here's the 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>

I updated my answer. Have a look again :)