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

Matthew Costigan
18,319 PointsSelect the first list item element and store it in the variable firstListItem
<!DOCTYPE html> <html> <head> <title>DOM Manipulation</title> </head> <link rel="stylesheet" href="style.css" /> <body> <ul> <li id="first">First Item</li> <li id="second">Second Item</li> <li id="third">Third Item</li> </ul> <script src="app.js"></script> </body> </html>
js let myList = document.getElementsByTagName('ul')[0]; let firstListItem;
13 Answers

NiKole Maxwell
11,083 Pointslet firstListItem = document.getElementById('first');
("first") is the id coming from the HTML tag on the Index file provided.

Brent Lane
5,778 PointsAll acceptable and passable answers below. All will pass, but the expectation is to use the first method as the video is focused on it.
let firstListItem = document.querySelector('li:first-child');
let firstListItem = document.querySelectorAll('li')[0];
let firstListItem = document.querySelector('li', [0]);
let firstListItem = document.getElementsByTagName("li")[0];
Good luck all!

Shawn Murray
1,997 PointsThe best way to answer this question is by performing the following:
let myList = document.querySelector("ul");

Vassil Kurktcheiv
3,248 Pointslet firstListItem = document.querySelector('li', [0]);

CHRISTOPHER CONNOR
4,740 Pointslet firstListItem = document.querySelectorAll("li")[0]; worked for me.

Jordan Watson
14,738 PointsSometimes when storing objects in variables etc its good to use jQuery that way its a case of doing the following.
var myList = $('ul'),
firstListItem = myList.find('l:first-item');
or if you don't want to use jQuery then
var myList = document.getElementsByTagName('ul')[0],
firstListItem = $document.getElementById('first');
You have an id on the first list item so you can target that for ease.
EDIT:
Or you could also use
document.querySelectorAll("li:first-child");

shashi7
11,034 PointsThis also passed the challenge....
let firstListItem = document.querySelector("ul li");
Since the Document method querySelector() returns the first Element within the document that matches the specified selector.

Siobhan Sheehan
7,882 Pointslet firstListItem = document.querySelector("li");
this passed for me

Jose A Reynaldo Vazquez
21,313 PointsThis is an easy way: let firstListItem= document.getElementById("first");

yk2
8,550 PointsI was able to get by like this:
let myList = document.querySelector('ul'); let firstListItem = document.querySelector('li, first');

TIMOTHY VASHUMANI GORORO
15,594 Pointsvar firstListItem = myList.querySelector('#first');

Brenda Cardona
8,397 PointsI tried different ways but none of them worked until I used the # to indicated a ID var firstListItem = document.querySelector('#first'); since it was only one ID with #first I was able to use it

Luke Winthorpe
Courses Plus Student 10,871 PointsI really struggled with using the first-child method and had to opt for TagName method instead
let firstListItem = document.getElementsByTagName("li")[0];
If anyone knows where I am going wrong with document.querySelector("li:first-child"); or cant it be used in this scenario?

CHRISTOPHER CONNOR
4,740 PointsI'm still learning and replying on my phone so can't confirm.
But would - document.querySelectorAll - work? As there is more than one li element and you're wanting to select the first one?