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
Jacob Richardson
2,969 PointsJavascript and the Dom - Traversing the Dom - Sibling Traversal Challenge
I have run this code a million times and it works, but the challenge is failing....any ideas?
var list = document.getElementsByTagName('ul')[0];
list.addEventListener('click', function(e) { if (e.target.tagName == 'BUTTON') { let li = event.target.parentNode; let nextLi = li.previousElementSibling; let ul = li.parentNode; let lastLi = ul.lastElementChild;
if(nextLi){
let p = nextLi.firstElementChild;
p.className = "highlight";
}
if(!nextLi){
let p = lastLi.firstElementChild;
p.className = "highlight";
}
} });
Steven Parker
243,134 PointsAlternately, since you've already posted this one, provide a link to the course page you were working on.
1 Answer
Steven Parker
243,134 PointsI'm working from memory since you didn't provide a link to the challenge, but I recall the instructions tell you to highlight the paragraph that comes before the button in the same list item. But I can see that this code would highlight the paragraph in the previous list item.
Hint: You're working too hard, the actual solution doesn't require traversing to the parent element or testing its position in the list.
Mark Wilkowske
Courses Plus Student 18,131 PointsMark Wilkowske
Courses Plus Student 18,131 PointsTry asking the question directly from the challenge. This way Treehouse can build navigation so we can see where this is and go from there. Thanks