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

Kate C
Kate C
13,589 Points

What does it mean cannot access 'listDiv' before initialization? and how to make it work?

1 Answer

cannot access 'listDiv' before initialization? Means you need to declare and assigned the variable before you will be able to use it.

and this is the correct way:

// you need to assigned (listDiv) first, then ues it.
const listDiv = document.querySelector('.list');
const listUl = listDiv.querySelector('ul');


// Using parentNode to Traverse Up the DOM
listUl.addEventListener('click', (event) => {
  if(event.target.tagName == 'BUTTON'){
    // let button = event.target
    let li = event.target.parentNode;
    let ul = li.parentNode;
    ul.removeChild(li);
  }
});