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

mickey Odunikan
mickey Odunikan
4,983 Points

There is an error with the code

There was an error with your code: TypeError: 'undefined' is not a function (evaluating 'document.getElementByClassName('last_name')')

app.js
var fullName= document.getElementById("full_name");
var lastName= document.getElementByClassName('last_name');
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>

2 Answers

Kelly von Borstel
Kelly von Borstel
28,880 Points

I think they want you to select the element by tag name. And since it's the second span tag it would have an index of 1, like this:

var fullName = document.getElementById("full_name");
var lastName = document.getElementsByTagName("span")[1];
mickey Odunikan
mickey Odunikan
4,983 Points

haha duh thanks I completely forgot about that

mickey Odunikan
mickey Odunikan
4,983 Points

now it is saying that the first step is wrong.

which was the id method

Kelly von Borstel
Kelly von Borstel
28,880 Points

Hi Mickey, I just copied and pasted my code into the challenge and it passed. You might have a typo. If you post your code again I can take a look and see if I notice any.

mickey Odunikan
mickey Odunikan
4,983 Points

Yup typo got it now thanks again!!!

Kelly von Borstel
Kelly von Borstel
28,880 Points

Just a note about the error hints from the system: If you make an error in task two, it will often say that task one is incorrect, even if you don't change anything at all to do with task one. The error hints are not always completely accurate or helpful.

Why did you choose document.getElementByClassName for your second line? this returns a collection since class can be applied to multiple elements

mickey Odunikan
mickey Odunikan
4,983 Points

how else can I select the second span element if the is no other specifier