JavaScript JavaScript and the DOM 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,972 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
Omar Ocampo
Omar Ocampo
3,165 Points

Thanks for your help boddy.

Omar Ocampo
Omar Ocampo
3,165 Points

This was my correct answer

parent.removeChild(removeMe);