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
SAMUEL LAWRENCE
Courses Plus Student 8,447 Points'Getting the parentNode of a parentNode'.
What exactly does Guil Hernandez mean when he says, Notice how I'm getting the parentNode of a parentNode?
listUL.addEventListener('click', (event) => {
if (event.target.tagName == 'BUTTON') {
let li = event.target.parentNode;
let ul = li.parentNode;
ul.removeChild(li);
}
});
He was referring to this 2 lines of codes
let li = event.target.parentNode;
let ul = li.parentNode;
Here's the link to the video: https://teamtreehouse.com/library/using-parentnode-to-traverse-up-the-dom
Time: 04:17
3 Answers
Steven Parker
243,318 Points Hi, I was notified by your tag.
This code does use a delegated handler (with "event bubbling"), but that's not related to the DOM traversal being done here. The traversal code would work the same in a direct handler.
So here's those two lines again with comments added:
let li = event.target.parentNode; // <-- this gets the parent of the button (event target)
let ul = li.parentNode; // <-- this gets the parent of the "li"
So "ul" is then the parent of the parent of the button (the event target).
Osaro Igbinovia
Full Stack JavaScript Techdegree Student 15,928 PointsHi Samuel, what Guil means is that he's using event bubbling to target the parent of the remove button (which is the list item that contains the name, confirmed checkbox and the remove button) and then target the parent of that particular list item (that contains the clicked remove button) which is the unordered list by making use of the event object. He could make us of event bubbling and the event object because he attached the event handler to the parent of all list items in the RSVP app which is the unordered list with an id of 'invitedList', this allows the event object handle the same event on its children.
Steven Parker
243,318 PointsFor the benefit of other readers, and to give yourself the best chance at more responses, it's always a good idea to start a new question for a change of topic.
But since it's already here this time ...
You can reduce the code by using a single delegated handler attached to the nearest common parent (the selection in this case), and filtering on the elements you want to react to:
const sect = document.querySelector("section");
sect.addEventListener("click", e => {
if (e.target.id.startsWith("gradient")) { // one of the DIVs was clicked
mydiv = e.target;
mybtn = mydiv.children[0];
} else if (e.target.tagName == "BUTTON") { // an inner button was clicked
mybtn = e.target;
mydiv = mybtn.parentNode;
} else return;
if (mydiv.style.height == "100%") {
mybtn.textContent = mybtn.textContent.replace("less", "more");
mydiv.style.height = "70%";
mydiv.style.transition = "height .5s";
mydiv.style.background = "linear-gradient(0deg, #fff, #fff, transparent 50%)";
} else {
mybtn.textContent = mybtn.textContent.replace("more", "less")
.replace("Get to Know", "Show less about");
mydiv.style.height = "100%";
mydiv.style.transition = "height .5s";
mydiv.style.background = "linear-gradient(0deg, #fff, #fff, transparent 30%)";
}
});
Also, instead of covering up the text with an overlay (but still having it take up space), I might place the text inside the element that expands and collapses. Then you wouldn't need the absolute positioning, either.
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsHi Steven Parker this works great but I see a lot of methods that I know nothing about, so this is advanced for me. I'm sure I will get it later on as I continue this. I understand some of it.
And in future I will post new topics in the forum. What you said makes sense.
Thanks a lot.
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsSAMUEL LAWRENCE
Courses Plus Student 8,447 PointsSo the parent of the button is the li and ul is the parent of li and the button? Is that what you mean? So he's targeting the parent (ul) which is the parent of li, which is the parent of button?
But then I have another question. Why was it necessary to do that? And in an unrelated matter, I'm trying to use this DOM traversing to solve a problem and it's giving me a headache.
so here's my code for my website I'm working on.
What I'm trying to do, as you probably figured out, is make the
show more/less about Sambutton slide down and up respectively revealing more or showing less when clicked. Sort of like thetoggleListexample we did a few lessons ago. I should be able to do it with the amount of javaScript I've done by now, but can't figure out how to do it using DOM traversal. I got it working with the code above, but as you can see it's a lot of unnecessary code. Also while going over my notes trying to figure this out, I realized the lesson where we added theremove itembutton that removed a list item when clicked, won't work if you had two listDiv. I know because I copied and pasted the list div in the html, but it only worked on the first listDiv.video link for toggleList: https://teamtreehouse.com/library/styling-elements
video link for remove item button: https://teamtreehouse.com/library/using-parentnode-to-traverse-up-the-dom
So my questions are,
event.targetmethod.