Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Traversing the DOM Using parentNode to Traverse Up the DOM

Rochelle Burrows
Rochelle Burrows
8,528 Points

Calling 'parentNode' on list item returns 'null'.

Hi everyone,

I'm not sure what's happening with my code; I'm trying to make the 'up' button in my app work, but when I call the 'parentNode' property on the 'li' element, it returns 'null', so I can't move further with using the 'insertBefore' method on the parent, since according the my code somewhere, the 'ul' parent element doesn't exist! Help with this is appreciated!

Thank you!

<div class="list">
      <p class="description">Things that are purple:</p>
      <input type="text" class="description">
      <button class="description">Change list description</button>
      <ul>
        <li>grapes
          <button class="up">up</button>
          <button>remove</button>
        </li>
        <li>amethyst 
          <button class="up">up</button>
          <button>remove</button>
        </li>
        <li>lavender 
          <button class="up">up</button>
          <button>remove</button>
        </li>
        <li>plums 
          <button class="up">up</button>
          <button>remove</button>
        </li>
      </ul>
      <input type="text" class="addItemInput">
      <button class="addItemButton">Add item</button>
    </div>
listUl.addEventListener('click', (event) => {
  if (event.target.tagName == 'BUTTON') {
    let li = event.target.parentNode;
    let ul = li.parentNode;
    // remove list item from list.
    ul.removeChild(li);
  }

  if(event.target.className == 'up'){
    let li = event.target.parentNode;
    let parentUl = li.parentNode;
    let prevElement = li.previousElementSibling;
    parentUl.insertBefore(li, prevElement);
  }
});

1 Answer

Emmanuel C
Emmanuel C
10,635 Points

Hey Rochelle,

I didnt see it in your code but i assumed that the variable listUl was equal to the only <ul> in your html.

When they click the up button both if statements are actually running, since the target.tagName is a button and it has the class of "up", So in the first if, the li is being removed from the ul with removeChild. Then when it gets to the second if and tries to call the parentNode of that same li, it returns null since it was removed as a child.

One way of dealing with this is giving a class to the remove button then you can check if the target.className is "remove" which is what i can only assume is suppose to remove the li.

Hope this helps. Good luck!

Rochelle Burrows
Rochelle Burrows
8,528 Points

Emmanuel - thank you so much for such a swift and perfectly explained response. It worked!

Extremely grateful!