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 trialMarie Josée Laporte
8,188 PointsChallenge Parent Traversal
Hi I wonder what is wrong with my code:
<!DOCTYPE html>
<html>
<head>
<title>Parent Traversal</title>
</head>
<link rel="stylesheet" href="style.css" />
<body>
<ul>
<li>Hello</li>
<li>Hi</li>
<li class="remove_me">Good bye!</li>
<li>Howdy</li>
</ul>
<script src="app.js"></script>
</body>
</html>
What my problem is this second task: Remove the removeMe element from the parent element.
This is what I tried:
const removeMe = document.querySelector('.remove_me');
let parent = removeMe.parentNode;
parent.addEventListener('click', (event) => {
if (event.target.tagName == 'REMOVEME') {
let li = event.target.parentNode;
let ul = li.parentNode;
ul.removeChild(li);
}
});
What did I do wrong?
const removeMe = document.querySelector('.remove_me');
let parent = removeMe.parentNode;
parent.addEventListener('click', (event) => {
if (event.target.tagName == 'REMOVEME') {
let li = event.target.parentNode;
let ul = li.parentNode;
ul.removeChild(li);
}
});
<!DOCTYPE html>
<html>
<head>
<title>Parent Traversal</title>
</head>
<link rel="stylesheet" href="style.css" />
<body>
<ul>
<li>Hello</li>
<li>Hi</li>
<li class="remove_me">Good bye!</li>
<li>Howdy</li>
</ul>
<script src="app.js"></script>
</body>
</html>
2 Answers
Ella Ruokokoski
20,881 PointsIt looks like you have over-complicated this a bit. All you need to do is to use the removeChild() method
parent.removeChild(removeMe);
Marie Josée Laporte
8,188 Pointsyes of course! much simpler... IT's not the first time I over-complicate thing! Thank you very much...