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

Not able to understand how to remove the list item element stored in firstListItem from the DOM.

When it says list item element does it mean the entire list stored in "ul". I am not able to remove the list item element using

firstListItem.removeChild(li)

Thanks for help

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.getElementsByTagName('ul')[0];
let firstListItem = document.getElementById('first');


 firstListItem.removeChild('ul');

You set firstListItem to document.getElementById('first') that just sets firstListItem to <li id="first">First Item</li>

firstListItem doesn't have any children so it does nothing.

What you want is

let myList = document.getElementsByTagName('ul')[0];
myList.removeChild(first);
Steven Parker
Steven Parker
231,261 Points

Not quite — the name of the variable that has the reference to to child element is not "first" but "firstListItem".

1 Answer

Steven Parker
Steven Parker
231,261 Points

To use "removeChild", you need to call the method on the parent, and pass the child being removed as the argument.

In the previous tasks you have selected both the parent and child elements and stored them in variables, so now you can use those variables to call the method.

You won't need any string literals for the last task.