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 Using parentNode to Traverse Up the DOM

Kate C
Kate C
13,589 Points

Why we don't need to write let button = event.target and just start with let li = event.target.parentNode;?

Because previously when listDiv is a parent, we write let li = event.target?

4 Answers

If I understood your question correctly, the reason why you start with

let li = event.target.parentNode;

is because you are trying to get the li element that has the button 'Remove' nested inside it. It looks something like this:

<li>
    <button>Remove</button>
</li>

as you can see, the button is a child of li element and button's parent node is li element.

The reason why previously you used this

let li = event.target;

is because you did not have a nested button inside the li element. And the way you removed the li elements is by clicking on them, which is okay but it would be better to use a button in order to remove the li elements. That is why you later created a button inside the li element and used to target the li element.

let li = event.target.parentNode;

would be the button that you are clicking on

event.target

would be the button's parent, which is li element.

.parentNode

Hopefully this answers your question. Feel free to ask me anything else :)

Kate C
Kate C
13,589 Points

Thank you Stivan!

I guess what i wanted to ask is -

we know li is ul 's child, but the teacher wrote let li = event.target; anyway.

listDiv.addEventListener('click', (event) => {
  if(event.target.tagName == 'LI'){
    let li = event.target;
    let ul = li.parentNode;
    ul.removeChild(li);
  }
});

and why we don't wrote let button = event.target , tho we know button is the child of li (and li is the child of ul)

listUl.addEventListener('click', (event) => {
  if(event.target.tagName == 'BUTTON'){
    // let button = event.target   (why we can ommit this line?) 
    let li = event.target.parentNode;
    let ul = li.parentNode;
    ul.removeChild(li);
  }
});

Ok, I got you:

// Both of these them are the same apart from the variable naming
let button = event.target;
let li = event.target;
  • They both would act the same way.
  • The reason why he added .parentNode is because he is trying to target, in this scenario, the list element.
  • if he did not add the .parentNode then if you where to click for example, on the button it will only remove the button, and not the list element.

  • I can see what you are trying to say. And hopefully this would answer your question

let button = event.target; // would not necessarily target the button on the page
// event.target itself is what causes the button to be targeted
// you can name the variable whatever you like, for example

let test = event.target;
let button325632 = event.target;
let y634g3g34g34gv34 = event.target;

// these will all target the button as long as they are in the scope of <div class='list'> due
// to the if statement

event.target.tagName === 'BUTTON'

// event.target becomes whatever you click on the page, for example
// if you click on the list element <li>, it will be targeted, meaning it's selected
// if you click on the button <button>, the button is now selected and can be referenced by the event.target
// You probably got confused because he set the variable to li
let li = event.target;
// So you thought that you can target the button by giving the variable name button
let button = event.target;
// which is not true

// You can see here that we set a listener 
listDiv.addEventListener('click', (event) => {

})

// Which means that it is listening for 'clicks' within the listDiv container
// So every time you click within the listDiv container, it will select WHATEVER you clicked on
// thanks to the event.target
  • Hopefully this makes sense, let me break it a little further.
<html>
    <body>
        <div class='list'>
            <ul>
                <li> grapes
                    <button>Remove</button>
                </li>
            </ul>
        </div>
    </body>
</html>
  • if you look at the html code above, it looks similarly to the 1 that you are doing.
  • So as you can see the "<div class='list'>", this is the container that holds the ul, li, button elements.
  • At the top of the code, we gave it a variable name listDiv
const listDiv = document.querySelector('.list');
  • Now we have a reference point, listDiv. It will reference to the <div class='list'> in the html code.
  • Just like above we will set a listener to listen for clicks within the listDiv container
listDiv.addEventListener('click', (event) => {

})

// This is our scope now, we can click anything within this scope and have it selected
// due to event.target

//<div class='list'>
//            <ul>
//               <li> grapes
//                    <button>Remove</button>
//                </li>
//            </ul>
//        </div>

// If we click the li element, it becomes selected
// if we click the button element, it becomes selected
// and so on
// We store those selections in variable, like this
let li = event.target;

// we choose convenient names like, li, button, div, only so that we can quickly find out what we are referring to
// the variable names, li, button, div, does not mean that we are directly selecting those elements
// whether we select the li, button, div is purely based on the event.target event.
// So as you can see
//               <li> grapes
//                    <button>Remove</button>
//                </li>
// the button is nested inside the list element tag, and since we are trying to remove the list element by clicking
// on the button, we first need to store that event in a variable so that we have a reference to where is and which list 
// element we are trying to remove.
let li = event.target.parentNode;
// this variable conveniently named li is set to the event.target which would be the button that we clicked and we are
// getting the parent node of the button, again it does not matter what you name the variable to be
// all of these WILL target the BUTTON because we CLICKED on it
let button534tm34fgm34 = event.target.parentNode;
let lgfdbhddbhdfbdf = event.target.parentNode;
let gregertg4gt34t43 = event.target.parentNode;
// the button element itself is store in the event.target when we click on the button.
//  and we use the .parentNode because we want to get the list element
// and the way we do it is by getting the button's parent, which just happens to be the list element 
// why is button's parent the list element?
// because the button is nested inside the list element.

Hopefully this help you understand event.target better. :)

Kate C
Kate C
13,589 Points

Thank you Stivan. Let me digest all the information!

I think we should have a clap or heart button to click on when we see an exceptionally helpful answer.