JavaScript JavaScript and the DOM Traversing the DOM Parent Traversal

kingdavid igbayilola
kingdavid igbayilola
8,678 Points

javascript DOM

i am getting confused here with traversal

app.js
const removeMe = document.querySelector('.remove_me');
let parent.addEventListener('click', (event)=> {
  if (event.target.tagName == 'LI') {
    let li = event.target;
  let ul = li.parentNode;
      }
});
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

Steven Parker
Steven Parker
172,034 Points

Task 1 says, "On line 2 of app.js, traverse to the parent element of the removeMe element.". This will only involve making an assignment to the "parent" variable. You won't need to establish an event listener, use any conditional code, or create any other variables. And as mentioned in the instructions, only one new line of code will be added.

And when you get to task 2, it also will need only one additional line of code.

You just need to change line 2 only. “Let parent = removeMe.parentNode;”