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 DOM Scripting By Example Editing and Filtering Names States of the Application

Nicholas Wallen
Nicholas Wallen
12,278 Points

Within the ul event listener, why is li referred to as a parent of ul? Isn't it the opposite?

ul.addEventListener('click', (e) => {

if (e.target.tagName === 'BUTTON') {

const button = e.target;

const li = button.parentNode;

const ul = li.parentNode;

4 Answers

From what you posted it looks like li is the parent of button and ul is the parent of li.

Othneil Drew
Othneil Drew
22,421 Points

"parentNode" is telling the browser to traverse the DOM to get the parent of the selected element.

const li = button.parentNode is finding the parent of the button and setting the li value to the button's parent. In this case the li element.

const ul = li.parentNode if finding the parent of the button and setting the ul value to the li's parent. In this case the ul element.

Andrew Tiller
Andrew Tiller
4,182 Points

<ul> is an "Unordered List" of <li> "List Items" which contain <button>s. button.parentNode will return the <li> the <button> is in. li.parentnode will return the <ul> that the <li> is in.

[element].parentNode doesn't assign something as a parent, it returns the parent of that element.

button.parentNode is you asking for 'the parent of this button', and li.parentNode is you asking for 'the parent of this list item', so as written the behaviour is as you are describing; ul is the parent of li, who in turn is the parent of button.