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) Traversing the DOM Parent Traversal

Challenge Task 2 of 2 Next, remove the removeMe element from the parent element.

Challenge Task 2 of 2 Next, remove the removeMe element from the parent element. Please give me the codes . I don't know what to do... HELP!

app.js
var removeMe = document.querySelector('.remove_me');
var parent = removeMe.parentNode;

('click', (event) => {
                   ();     
                        });
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Parent Traversal</title>
    </head>
    <link rel="stylesheet" href="style.css" />
    <body>
        <ul>
            <li>Hello</li>
            <li>Hi</li>
            <li class="remove_me">Good bye!</li>
            <li>Howdy</li>
        </ul>
        <script src="app.js"></script>
    </body>
</html>

2 Answers

Ethan Rivas
Ethan Rivas
9,979 Points

Hi mo ode ,

In the first part, the instruction and the code are this:

  • On line 2 of app.js, traverse to the parent element of the removeMe element.
1. var removeMe = document.querySelector('.remove_me');
2. var parent;

So, you'll need to add in the second lane (parent variable): var parent = removeMe.parentNode;

Now, in the second part:

  • Next, remove the removeMe element from the parent element.

The only thing you'll need to do right here is select the parent (which is defined in the second line as a variable) and remove the removeMe element (first line variable) as parameter.

The answer for this challenge should look like this:

1. var removeMe = document.querySelector('.remove_me');
2. var parent = removeMe.parentNode;
3. 
4. parent.removeChild(removeMe);

Hope this helped you to understand what the challenge was asking you to do. I'll leave you some references if you want to read a little bit more about this:

  1. https://webplatform.github.io/docs/dom/Node/parentNode/
  2. https://stackoverflow.com/questions/13763/how-can-i-remove-a-child-node-in-html-using-javascript

Thanks for your help boddy.

This was my correct answer

parent.removeChild(removeMe);