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 trialVal Nunez
2,763 Pointsdocument.getElementByTagName doesn't seem to be working with second SPAN last_name? Not sure what I'm doing wrong, gosh.
Am I missing something? I probably am, haha! Oh man, any help would be greatly appreciated, thank you in advance!
var fullName = document.getElementById("full_name");
var lastName = document.getElementsByTagName("last_name");
<!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
ALBERT QERIMI
49,872 Pointsvar lastName= document.getElementsByTagName("span")[1];
Val Nunez
2,763 PointsThank you for your help! :)
Allison Hanna
36,222 PointsThe tag name is <span>
and the class name is last_name
. Perhaps you want to use document.getElementsByClassName
, var lastName = document.getElementsByClassName("last_name");
Val Nunez
2,763 PointsThank you very much for the help! It doesn't make sense to me yet, but I will keep practicing!
Cheo R
37,150 PointsCheo R
37,150 PointsFrom the MDN examples:
element : a reference to an Element object, or null if an element with the specified ID is not in the document.
id : a case-sensitive string representing the unique ID of the element being sought.
Where element would contain other stuff. Applying the example to your example:
Where fullName is an element that contains other stuff. So instead of calling getElementsByTagName on document, you call it on fullName. But just doing that won't it because you get a list of span objects, hence you need to select the second one at index [1].