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

Select 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

let firstListItem = document.getElementById('first');

("first") is the id coming from the HTML tag on the Index file provided.

All 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!

The best way to answer this question is by performing the following:

let myList = document.querySelector("ul");

let firstListItem = document.querySelector('li', [0]);

let firstListItem = document.querySelectorAll("li")[0]; worked for me.

Sometimes 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");

This 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.

let firstListItem = document.querySelector("li");

this passed for me

This is an easy way: let firstListItem= document.getElementById("first");

I was able to get by like this:

let myList = document.querySelector('ul'); let firstListItem = document.querySelector('li, first');

var firstListItem = myList.querySelector('#first');

I 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

I 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?

I'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?