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 JavaScript and the DOM (Retiring) Making Changes to the DOM Removing an Element from the DOM

Clinton Johnson
Clinton Johnson
28,714 Points

Remove the list item element stored in firstListItem from the DOM.

can someone please explain and post the code answer to the question i have tried everything and i having a hard time understanding.

index.html
<!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>
app.js
let myList = document.querySelector('ul');
let firstListItem = document.querySelector('li');
firstListItem.remove();

6 Answers

Hi,

Your code does what the challenge ask for. It removes the first li element from the unordered list. However to pass the challenge, you have to use the removeChild method instead of the remove method, So in order to pass the challenge, the last line of your code should be:

myList.removeChild(firstListItem);

The way I solved was:

// 1st task
let myList = document.querySelector('ul');
// 2nd task
let firstListItem = document.querySelector('li:first-child');
// 3rd task
myList.removeChild(firstListItem);

I hope it helps. Have a nice day

Clinton Johnson
Clinton Johnson
28,714 Points

Thank you Eduardo, that what i was missing was to set the the firstListItem as li:first-child and then remove it thank you for your explanation.

Fuad Muhammad
Fuad Muhammad
4,273 Points

Just give 'Best Answer' for Eduardo.

Nickolas Fuentes
Nickolas Fuentes
14,016 Points

Man I almost had it all the way! The question to me is asking to remove the firstListItem li which is stored in the firstListItem. So I thought this!

firstListItem.removeChild('li:first-child');

Which is why even store it in there in the first place haha! But thanks for cleaning this one up!

Maikal Kumar
Maikal Kumar
4,858 Points

document.querySelector('li');

by default querySelector() selects first child only. so it will also work fine .

Paulo Costa
Paulo Costa
2,407 Points

I´ve done it following the .getElementsByTagName. It would be:

Challenge Task 1 of 3 Select the unordered list element and store it in the variable myList

You need to store the element in the list ul = Unordered List

let myList = document.getElementsByTagName('ul')[0];

Challenge Task 2 of 3 Select the first list item element and store it in the variable firstListItem

Pick up the first one: li = List Item

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

Challenge Task 3 of 3 Remove the list item element stored in firstListItem from the DOM.

Let´s remove the item:

myList.removeChild(firstListItem);

this works: var firstListItem = document.querySelector('li, .first');